1

複数のユーザーの登録ページがあります。ユーザーが登録すると、ユーザー名とパスワードを含む複数のフォームデータでデータベースに保存されます。次に、ログインページでviewcontrollerに入ります。私がそれを使っusername=abcd&password=123ているとき。しかし、username=%@&password=%@複数のユーザーで使用すると機能しません。

コード:

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