0

ログイン画面があり、ユーザー名とパスワードをログインページに投稿します。

ログインユーザーの詳細が正しい場合、Webサービスは2つの応答を返します。リクエストから応答が返されます。

{"値":1}

ユーザーの詳細が間違っている場合は、リクエストから戻ってきます。

{"値":0}

そのJSON結果を解析して、次のログ出力を得ることができました

値: 1 または値:0

解析されたjsonを処理するために戦っています。

 //parse out the json data
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData //1
                                                     options:kNilOptions
                                                       error:&error];

NSArray* defineJsonData = [json objectForKey:@"value"]; //2

NSLog(@"value: %@", defineJsonData); //3

if ([[json objectForKey:@"value"] isEqualToNumber:[NSNumber numberWithInt:1]])
{

    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:0];
    [self performSegueWithIdentifier: @"introScreenView" sender:self];
}

else {
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:0];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"Please try to login again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

これが私のコードの残りの部分です。

- (void)myTask {

if ([userNameTextField.text isEqualToString:@""] || [passwordTextField.text isEqualToString:@""]) {
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:0];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Feilds Missing" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    return;
}
NSString *data = [NSString stringWithFormat:@"UserName=%@&Password=%@",userNameTextField.text, passwordTextField.text];
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

// preaparing URL request to send data.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSString *url = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Account/LogOnIOS?"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];

NSURLResponse *response;
NSError *error;

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"Login response:%@",str);

NSHTTPCookie *cookie;
NSLog(@"name: '%@'\n",   [cookie name]);

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}


//parse out the json data
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:urlData //1
                                                     options:kNilOptions
                                                       error:&error];

NSArray* defineJsonData = [json objectForKey:@"value"]; //2

NSLog(@"value: %@", defineJsonData); //3

if ([defineJsonData isEqual:0])
{
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:0];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"Please try to login again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

else {

    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:0];
    [self performSegueWithIdentifier: @"introScreenView" sender:self];
}


if (theConnection) {


}
}
4

1 に答える 1