1

I have registration page for multiple users. When user register it will stores to database in multiple form data with username and password. Then i'm getting in viewcontroller with login page. When i use username=abcd&password=123 it working. But when i use username=%@&password=%@ for multiple users it's not working.

code:

NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[usrname text],[password text]];
NSLog(@"PostData: %@",post);

NSURL *url=[NSURL URLWithString:@"http://myserver.net/projects/mobile/test_login.php?name=abcd&password=123"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"Response code: %d", [response statusCode]);

if ([responseData length]){
    NSLog(@"Response ==> %@", responseData);
    NSMutableDictionary *dict=[responseData JSONValue];
    NSInteger success = [(NSNumber *) [dict objectForKey:@"success"] integerValue];

    NSLog(@"%d",success);
        if(success == 1){
            //save the ID
            NSInteger id1 = [(NSNumber *) [dict objectForKey:@"id"] integerValue];
            NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
            [userData setInteger:id1 forKey:@"id"];
            [userData synchronize];

            NSLog(@"id1 data is %@",userData);

            NSLog(@"Login SUCCESS");
            [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
            [self.navigationController pushViewController:overlay animated:YES];                        
        } else {
            NSString *error_msg = (NSString *) [dict objectForKey:@"error_message"];
            [self alertStatus:error_msg :@"Login Failed!"];
        }
    }
D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
user2674668
  • 117
  • 2
  • 13
  • what is you PostData: log when you run this? – D-eptdeveloper Sep 10 '13 at 06:05
  • It displays the UITextfield typing charactors – user2674668 Sep 10 '13 at 06:16
  • okay great and please explain what do you mean by "for multiple users it's not working"? – D-eptdeveloper Sep 10 '13 at 06:22
  • In this line NSURL *url=[NSURL URLWithString:@"http://myserver.net/projects/mobile/test_login.php?name=abcd&password=123"]; I used abcd & 123 for textfield. That's why it allow. If i didn't use anything it won't allow – user2674668 Sep 10 '13 at 06:35
  • so pass your textfields values in place of abcd & 123 in that url for webservice. – D-eptdeveloper Sep 10 '13 at 06:38
  • Yes. If i use this http://myserver.net/projects/mobile/test_login.php it won't allow any username & password...I referred from here http://dipinkrishna.com/blog/2012/03/iphoneios-programming-login-screen-post-data-url-parses-json-response/5/ – user2674668 Sep 10 '13 at 06:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37082/discussion-between-eptdeveloper-and-user2674668) – D-eptdeveloper Sep 10 '13 at 06:55

1 Answers1

1

you should try it this way

NSURL *url=[NSURL URLWithString:[NSString   stringWithFormat:@"http://myserver.net/projects/mobile/test_login.php?name=%@&password=%@",[usrname text],[password text]]];

hope it helps you thanks. :)

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30