当我将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以前的版本,但当我更新到Xcode 7.0,在iOS 9.0上,它开始给我的问题如下,当我调用上面的web服务方法。我得到的日志错误是:

Connection failed: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7fada0f31880 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=MyServiceURL, NSErrorFailingURLKey=MyServiceURL, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}

我已经尝试了以下问题和答案,但没有得到任何结果,有任何提前的想法,我可以如何删除服务呼叫错误?

无法加载的资源为ios9 应用程序传输安全Xcode 7 beta 6 https://stackoverflow.com/a/32609970


当前回答

适用于iOS 10。x和Swift 3。X[也支持以下版本]只需在'info.plist'中添加以下行

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

其他回答

无法加载资源,因为应用程序传输安全策略要求使用在Swift 4.03中工作的安全连接。

打开你的pList.info作为源代码,并粘贴:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

iOS 9.0或更高版本、OS X v10.11及更高版本提供传输安全性。

默认情况下只允许在应用中调用https。要关闭应用程序传输安全,在信息中添加以下行。plist文件…

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>

欲了解更多信息: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33

建议只在app中使用https调用。iOS 9.0引入了传输安全性,默认情况下只允许应用程序通过https访问。

但是,如果您仍然希望允许所有非https调用- 在信息中添加NSAppTransportSecurity (Dictionary)。plist和 添加名为NSAllowsArbitraryLoads的子键,其布尔值为YES。 更新后,plist应该看起来像

步骤

打开Info.plist 单击Information属性列表下的+ 将应用程序传输安全设置(NSAppTransportSecurity)添加为字典 单击添加的应用程序传输安全设置下的+ 添加允许任意负载(NSAllowsArbitraryLoads)为布尔值,并将值设置为YES

如果你使用的是Xcode 8.0和swift 3.0或2.2

如果你使用firebase,它会在NSAppTransportSecurity部分中添加NSAllowsArbitraryLoadsInWebContent = true,而NSAllowsArbitraryLoads = true将不起作用