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!"];
        }
    }
4

1 回答 1

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. :)

于 2013-09-10T07:05:24.207 回答