【发布时间】:2017-12-19 19:26:54
【问题描述】:
我正在尝试读取NSHTTPURLResponse 状态代码,但是NSHTTPURLResponse 返回nil,但没有创建NSError。
这在 iOS 11 之前有效,但我没有收到关于它已被弃用的警告,并且我无法找到任何在线引用 NSURLSession 的内容。
知道为什么吗?
我一般调用下面的方法[MyClass getHTTPResponseRequest:@"http://www.google.com/"];
+ (NSInteger) getHTTPResponseRequest : (NSString*) testURL {
__block NSHTTPURLResponse * r = nil;
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:testURL]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error %@", error.localizedDescription);
}
r = (NSHTTPURLResponse *)response;
}] resume];
if(r == nil){
NSLog(@"Response is nil");
return 9999;
}
return r.statusCode;
}
【问题讨论】:
标签: ios objective-c objective-c-blocks nsurlsession nserror