I'm am a complete newbie to iOS development and am working on my first application. I am interfacing with a web service that requires a user to login with their username and password. I want to check and make sure that they have entered the correct username and password before I save it in keychain so I'm making a simple get request with there credentials. I want to check the response I get and see if I get an error message or not. Here is the code I have written, which performs a GET request to the web service.
-(BOOL)checkCredentials:(NSString *)username withPassword:(NSString *)password{
NSString *requestString = @"some_web_service_url";
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];
NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};
self.session=[NSURLSession sessionWithConfiguration:sessionConfig];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", jsonObject);
}];
[dataTask resume];
//I think error checking logic should go here.
}
I want to check my jsonObject
for an error code but can I do it after I do [dataTask resume]
? Is there a better way to check the return code? I believe jsonObject
will return json so I think I want to check the header for a return value, but I'm not fully sure. Excuse me if this is an easy question but I'm new and a bit confused. Any help would be greatly appreciated!