Ios 检查NSURLSessionDataTask的响应值
我是iOS开发的新手,正在开发我的第一个应用程序。我正在与一个需要用户使用用户名和密码登录的web服务交互。在我将其保存到keychain之前,我想检查并确保他们输入了正确的用户名和密码,因此我将使用这些凭据发出一个简单的get请求。我想检查收到的响应,看看是否收到错误消息。下面是我编写的代码,它执行对web服务的GET请求Ios 检查NSURLSessionDataTask的响应值,ios,objective-c,nsurlsessionconfiguration,Ios,Objective C,Nsurlsessionconfiguration,我是iOS开发的新手,正在开发我的第一个应用程序。我正在与一个需要用户使用用户名和密码登录的web服务交互。在我将其保存到keychain之前,我想检查并确保他们输入了正确的用户名和密码,因此我将使用这些凭据发出一个简单的get请求。我想检查收到的响应,看看是否收到错误消息。下面是我编写的代码,它执行对web服务的GET请求 -(BOOL)checkCredentials:(NSString *)username withPassword:(NSString *)password{ N
-(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.
}
我想检查我的jsonObject是否有错误代码,但我可以在执行[dataTask resume]之后再执行吗?有没有更好的方法来检查返回码?我相信jsonObject将返回json,所以我想检查头部是否有返回值,但我不能完全确定。如果这是一个简单的问题,请原谅,但我是新来的,有点困惑。任何帮助都将不胜感激 您可以通过在会话数据任务完成块中将NSURLResponse强制转换为NSHTTPURLResponse并检查statusCode属性来检索HTTP返回代码
- (void)viewDidLoad {
[super viewDidLoad];
NSString *user; // Update to your user name
NSString *password; // Update to your use name password
[self checkCredentials:user withPassword:password completion:^(BOOL authorized) {
if (authorized) {
// Save the credentials
dispatch_async(dispatch_get_main_queue(), ^{
// Update your UI if needed.
});
} else {
// Unauthorized
//
// Inform the user if needed.
}
}];
}
- (void)checkCredentials:(NSString *)username
withPassword:(NSString *)password
completion:(void (^)(BOOL))authorized
{
NSString *requestString; // Update to your 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) {
NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse *)response;
NSUInteger statusCode = httpURLResponse.statusCode;
switch (statusCode) {
case 200: // Authorized
authorized(YES);
break;
case 401: // Unauthorized
authorized(NO);
break;
default:
// Unauthorized
//
// For a copmlete list of HTTP response status codes see http://www.ietf.org/rfc/rfc2616.txt
// also be aware that not all Web Services return the same status codes.
authorized(NO);
break;
}
}];
[dataTask resume];
}