当我将 Xcode 更新到 7.0 或 iOS 9.0 时,我遇到了这个问题。
不知怎的,它开始给我标题错误
“无法加载资源,因为应用程序传输安全性
策略要求使用安全连接”
网络服务方法:
- (void)ServiceCall:(NSString*)ServiceName :(NSString *)DataString
{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfiguration setAllowsCellularAccess:YES];
[sessionConfiguration setHTTPAdditionalHeaders:@{ @"Accept" : @"application/json" }];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",ServiceURL]];
NSLog(@"URl %@%@",url,DataString);
// Configure the Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:[NSString stringWithFormat:@"%@=%@", strSessName, strSessVal] forHTTPHeaderField:@"Cookie"];
request.HTTPBody = [DataString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = @"Post";
// post the request and handle response
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// Handle the Response
if(error)
{
NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
// Update the View
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the Loader
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] delegate].window animated:YES];
});
return;
}
NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];
for (NSHTTPCookie * cookie in cookies)
{
NSLog(@"%@=%@", cookie.name, cookie.value);
strSessName=cookie.name;
strSessVal=cookie.value;
}
NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
}
该服务对于 Xcode 早期版本和 iOS 早期版本运行良好但是当我更新到 iOS 9.0 上的 Xcode 7.0 时,当我调用上述 Web 服务方法时,它开始给我带来如下问题。我收到的记录错误是:
连接失败:错误域=NSURLErrorDomain代码=-1022“
由于应用程序传输安全策略,无法加载资源
需要使用安全连接。”
UserInfo={NSUnderlyingError=0x7fada0f31880 {错误
域=kCFErrorDomainCFNetwork代码=-1022“(空)”},
NSErrorFailingURLStringKey=我的服务URL,
NSErrorFailingURLKey=我的服务URL,
NSLocalizedDescription=无法加载资源,因为
应用程序传输安全策略要求使用安全的
联系。}
我已尝试以下问题和答案,但没有得到任何结果,是否有任何先进的想法如何消除该服务调用错误?
- ios9 无法加载资源 https://stackoverflow.com/questions/32069393/the-resource-could-not-be-loaded-is-ios9
- 应用程序传输安全 Xcode 7 beta 6 https://stackoverflow.com/q/32427300
- https://stackoverflow.com/a/32609970 https://stackoverflow.com/a/32609970