この方法の API を使用してアプリにログインしています。
NSString *url = [NSString stringWithFormat:@"%@//login",xyz];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (!jsonData) {
NSLog(@"Error creating JSON object: %@", [error localizedDescription]);
}
[request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:APIKEY forHTTPHeaderField:@"X_API_KEY"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request
// the NSOperationQueue upon which the handler block will be dispatched:
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error]; //I am using sbjson to parse
if(httpResponse.statusCode == 200)
{
//Show message to user success
}
else if(httpResponse.statusCode == 401)
{
//Show message to user -fail
}
else if(httpResponse.statusCode == 500)
{
//Show message to user- server error
}
}];
正しいユーザー名とパスワードを使用すると、httpResponse とステータス コードが 200 になります。しかし、間違ったユーザー名とパスワードを使用すると、401 が返されません。
では、この状況をどのように処理するか
よろしくランジット