无法加载资源,因为App Transport Security策略要求使用安全连接

Man*_*Mal 460 nsurlconnection ios nsurlsession ios9 xcode7

我将Xcode更新为7.0或iOS 9.0时遇到问题.不知怎的,它开始给我标题错误

"无法加载资源,因为App Transport Security策略要求使用安全连接"

Web服务方法:

-(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];

}
Run Code Online (Sandbox Code Playgroud)

该服务运行良好的Xcode早期版本和iOS以前的版本但是当我更新到iOS 9.0上的Xcode 7.0时,它开始给我一个问题,如我调用上述Web服务方法时跟随.我得到的记录错误是:

连接失败:错误Domain = NSURLErrorDomain Code = -1022"无法加载资源,因为App Transport Security策略要求使用安全连接." UserInfo = {NSUnderlyingError = 0x7fada0f31880 {Error Domain = kCFErrorDomainCFNetwork Code = -1022"(null)"},NSErrorFailingURLStringKey = MyServiceURL,NSErrorFailingURLKey = MyServiceURL,NSLocalizedDescription =无法加载资源,因为App Transport Security策略要求使用安全连接.}

我试过以下问题和答案,但没有得到任何结果,有任何提前的想法,我如何删除该服务调用错误?

  1. 无法加载的资源是ios9
  2. App Transport Security Xcode 7 beta 6
  3. /sf/answers/2282697931/

Man*_*Mal 893

我已经通过在info.plist中添加一些键来解决它.我遵循的步骤是:

  1. 打开我的Project目标info.plist文件

  2. 增加了一个名为关键NSAppTransportSecurityDictionary.

  3. 添加了一个名为NSAllowsArbitraryLoadsas 的子键Boolean,并将其值设置YES为如下图所示.

在此输入图像描述

清洁项目,现在一切正常,就像以前一样.

参考链接:https://stackoverflow.com/a/32609970

编辑: 或者在info.plist文件的源代码中我们可以添加:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourdomain.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <false/>
            </dict>
       </dict>
  </dict>
Run Code Online (Sandbox Code Playgroud)

  • 对于Xcode 8.3/Swift 3,它分别是"App Transport Security Settings"和"Allow Arbitrary Loads" (6认同)
  • 起初它不起作用.您还需要在Project> Target中添加键 (5认同)
  • 以下是一些有用的Apple文档https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33 (3认同)
  • @MihirOza:是的,传输安全是 iOS 9 的一项新功能。 (2认同)
  • 如果您没有异常域,只需删除“NSExceptionDomains”下的任何键。否则会成为`NSAllowArbitraryLoads`的异常,那么你必须使用https访问这个`exception` (2认同)

Arj*_*jan 261

请注意,使用NSAllowsArbitraryLoads = true允许与任何服务器的所有连接都不安全.如果要确保只能通过不安全的连接访问特定域,请尝试以下操作:

在此输入图像描述

或者,作为源代码:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>domain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>
Run Code Online (Sandbox Code Playgroud)

编辑后清理和构建项目.


Tej*_*ina 40

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

因此,默认情况下,仅在应用中允许https调用.要关闭App Transport Security,请在info.plist文件中添加以下行...

<key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问:https:
//developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33

  • 但是,这仍然不允许我对任何域进行 HTTP 调用。有任何线索为什么会发生吗? (3认同)

Mr.*_*ean 32

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

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)


Ena*_*que 25

在Swift 4中你可以使用

- >转到Info.plist

- >单击信息属性列表的加号

- >将App Transport Security设置添加为字典

- >单击加号图标应用程序传输安全设置

- >添加允许任意负载设置为YES

贝娄图像看起来像

在此输入图像描述


Avi*_*651 21

我已解决为plist文件.

添加NSAppTransportSecurity:Dictionary.

将名为"NSAllowsArbitraryLoads"的子项添加为布尔值:YES

在此输入图像描述


Sam*_*Sam 17

这是 Apple 强制对您的 apis 进行更严格安全的方式(被迫通过 http 使用 https)。我将解释如何删除此安全设置。


这里的大多数答案都指出将此密钥添加到您的 info.plist 在此处输入图片说明

仅此一项并没有为我解决这个问题。我不得不在里面添加相同的密钥

Project -> Targets -> Info -> Custom iOS Target Properties 在此处输入图片说明


然而,这将允许任何人发生不安全的连接。如果您只想允许特定域使用不安全连接,您可以将以下内容添加到您的 info.plist。

在此处输入图片说明


Kes*_*era 16

无法加载资源,因为App Transport Security策略需要使用在Swift 4.03中工作的安全连接.

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

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)


Ani*_*mar 11

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

在此输入图像描述


Ash*_*k R 11

来自Apple文档

如果您正在开发新应用,则应该专门使用HTTPS.如果您有现有应用,则应尽可能多地使用HTTPS,并创建一个计划,以便尽快迁移其余应用.此外,您通过更高级别的API进行的通信需要使用具有前向保密性的TLS 1.2版进行加密.如果尝试建立不符合此要求的连接,则会引发错误.如果您的应用需要向不安全的域发出请求,则必须在应用的Info.plist文件中指定此域.

绕过应用程序传输安全性:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
Run Code Online (Sandbox Code Playgroud)

允许所有不安全的域

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

阅读更多:在iOS 9和OSX 10.11中配置App Transport Security例外


A.G*_*A.G 9

在Xcode 7.1之后(swift 2.0)

在此输入图像描述


小智 9

在 XCode 12.5 中。IOS 14.我做了以下条目

在此输入图像描述

这是 info.plist 源代码的样子

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)


pie*_*e23 8

您只需要在URL中使用HTTPS而不是HTTP,它就可以使用

  • 当然,除非您不能使用HTTPS,因为Apple不喜欢自签名证书.你没有那个问题吗? (3认同)

MrW*_*med 7

如果您不是XML的忠实粉丝,那么只需在plist文件中添加以下标记即可.

在此输入图像描述


小智 5

iOS 9(可能)强制开发人员专门使用App Transport Security.我无意中听到了这个地方,所以我不知道这是否属实.但我怀疑它并得出了这个结论:

在iOS 9上运行的应用程序(可能)将不再连接到没有SSL的Meteor服务器.

这意味着运行meteor run ios或meteor run ios-device将(可能?)不再起作用.

在应用程序的info.plist中,NSAppTransportSecurity [Dictionary]需要NSAllowsArbitraryLoads [Boolean]设置一个密钥YES或Meteor需要尽快使用httpslocalhost server.


Md *_*ury 5

如果您使用的是Xcode 8.0至8.3.3,而Swift 2.2至3.0

就我而言,需要将URL http://更改为https:// (如果不起作用,请尝试)

Add an App Transport Security Setting: Dictionary.
Add a NSAppTransportSecurity: Dictionary.
Add a NSExceptionDomains: Dictionary.
Add a yourdomain.com: Dictionary.  (Ex: stackoverflow.com)

Add Subkey named " NSIncludesSubdomains" as Boolean: YES
Add Subkey named " NSExceptionAllowsInsecureHTTPLoads" as Boolean: YES
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


ISS*_*ISS 5

对于那些在本地主机上开发的人,请遵循以下步骤:

  1. 点击旁边的“+”按钮Information Property List并添加App Transport Security Settings并为其分配Dictionary类型
  2. 点击新创建的App Transport Security Settings条目旁边的“+”按钮并添加NSExceptionAllowsInsecureHTTPLoads类型Boolean并将其值设置为YES
  3. 右键单击NSExceptionAllowsInsecureHTTPLoads条目并单击“右移行”选项,使其成为上述条目的子项。
  4. 点击NSExceptionAllowsInsecureHTTPLoads条目旁边的“+”按钮并添加Allow Arbitrary Loads类型Boolean并将其值设置为YES

注意:它最终应该类似于下图所示

在此处输入图片说明