【问题标题】:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection无法加载资源,因为应用传输安全策略需要使用安全连接
【发布时间】:2015-12-14 08:57:26
【问题描述】:

当我将 Xcode 更新到 7.0 或 iOS 9.0 时,我遇到了这个问题。 不知何故,它开始给我标题错误

“无法加载资源,因为应用程序传输安全 策略要求使用安全连接”

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

}

Xcode 早期版本和 iOS 早期版本的服务运行良好但是当我更新到 iOS 9.0 上的 Xcode 7.0 时,当我调用上述 Web 服务方法时,它开始给我带来如下问题。我得到的记录错误是:

连接失败:错误域=NSURLErrorDomain 代码=-1022 "The 无法加载资源,因为应用程序传输安全策略 需要使用安全连接。” UserInfo={NSUnderlyingError=0x7fada0f31880 {错误 域=kCFErrorDomainCFNetwork 代码=-1022 "(null)"}, NSErrorFailingURLStringKey=MyServiceURL, NSErrorFailingURLKey=MyServiceURL, NSLocalizedDescription=资源无法加载,因为 应用传输安全策略需要使用安全的 连接。}

我尝试了以下问题和答案,但没有得到任何结果,是否有任何提前的想法可以消除该服务调用错误?

  1. The resource could not be loaded is ios9
  2. App Transport Security Xcode 7 beta 6
  3. https://stackoverflow.com/a/32609970

【问题讨论】:

标签: ios nsurlconnection nsurlsession ios9 xcode7


【解决方案1】:

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

这就是 info.plist 源代码的样子

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

【讨论】:

    【解决方案2】:

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

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

    2. 添加了一个名为NSAppTransportSecurity 的密钥作为Dictionary

    3. 添加了一个名为NSAllowsArbitraryLoads 的子键为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>
    

    【讨论】:

    • 一开始是不行的。您还需要在 Project > Target 中添加键
    • @MihirOza:是的,传输安全是 iOS 9 的新功能。
    • 如果您没有例外域,只需删除NSExceptionDomains 下的任何键。否则会是NSAllowArbitraryLoads的例外,那么你必须使用https访问这个exception
    • 对于 Xcode 8.3 / Swift 3 分别是 App Transport Security SettingsAllow Arbitrary Loads
    【解决方案3】:

    这是 Apple 在您的 api 上强制加强安全性的方式(强制使用 https 而不是 http)。我将解释如何删除此安全设置。


    这里的大多数答案都指出将此密钥添加到您的 info.plist

    仅此一项并没有为我解决这个问题。 我必须在里面添加相同的键

    Project -&gt; Targets -&gt; Info -&gt; Custom iOS Target Properties


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

    【讨论】:

    • 谢谢,这对我有用!我只编辑了之前不起作用的 plist 文件。我很惊讶其他人如何以这种方式工作
    【解决方案4】:

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

    【讨论】:

    • 感谢您提供此信息。您为我节省了数小时的故障排除时间。
    【解决方案5】:

    对于那些在 localhost 上开发的人,请按照以下步骤操作:

    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

    注意:最终应该如下图所示

    【讨论】:

      【解决方案6】:

      请注意,在项目的info.plist 中使用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>
      

      编辑后清理并构建项目。

      【讨论】:

      • 感谢您的回答,只是 NSAllowArbitaryLoads 对我不起作用,添加 NSExceptionAllowsInsecureHTTPLoads 可以解决问题
      • 我还添加了这两个键: NSTemporaryExceptionAllowsInsecureHTTPLoads
      【解决方案7】:

      在 Swift 4 中你可以使用

      ->转到 Info.plist

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

      ->将应用传输安全设置添加为字典

      -> 点击加号图标App Transport Security Settings

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

      下面的图片看起来像

      【讨论】:

        【解决方案8】:

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

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

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

        【讨论】:

          【解决方案9】:

          确保更改正确的 info.plist 文件

          这是我第二次在这个问题上浪费时间,因为我没有注意到我正在更改 MyProjectNameUITests 下的 info.plist。

          【讨论】:

            【解决方案10】:

            将您的 pList.info 打开为 Source Code 并在底部 &lt;/dict&gt; 之前添加以下代码,

             <!--By Passing-->
                <key>NSAppTransportSecurity</key>
                <dict>
                    <key>NSExceptionDomains</key>
                    <dict>
                        <key>your.domain.com</key>
                        <dict>
                            <key>NSIncludesSubdomains</key>
                            <true/>
                            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                            <true/>
                            <key>NSTemporaryExceptionMinimumTLSVersion</key>
                            <string>1.0</string>
                            <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
                            <false/>
                        </dict>
                    </dict>
                </dict>
                <!--End Passing-->
            

            最后将your.domain.com 更改为您的基本网址。谢谢。

            【讨论】:

              【解决方案11】:

              如果您使用的是 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
              

              【讨论】:

                【解决方案12】:

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

                【讨论】:

                  【解决方案13】:

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

                  因此,默认情况下,只有 https 调用只允许在应用中使用。要关闭 App Transport Security,请在 info.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

                  【讨论】:

                  • 但是,这仍然不允许我对任何域进行 HTTP 调用。有什么线索为什么会发生吗?
                  • 即使它是一个 HTTP URL,该 URL 也应该经过验证或者应该有一个 vlid SSL 证书。
                  【解决方案14】:

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

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

                  【讨论】:

                  • 对于 10.x 和 Swift 3.x,我在其他地方看到密钥实际上是 AppTransportSecurity 和 AllowsArbitraryLoads,但它们是错误的。这些键仍然需要 NS 前缀,即使是 Swift 3.x。当我依赖这些键的 NS 前缀时,错误就消失了。
                  【解决方案15】:

                  来自 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>
                  

                  允许所有不安全的域

                  <key>NSAppTransportSecurity</key>
                  <dict>
                    <!--Include to allow all connections (DANGER)-->
                    <key>NSAllowsArbitraryLoads</key>
                    <true/>
                  </dict>
                  

                  阅读更多:Configuring App Transport Security Exceptions in iOS 9 and OSX 10.11

                  【讨论】:

                    【解决方案16】:

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

                    【讨论】:

                      【解决方案17】:

                      在自托管解析服务器使用一年签名证书而不是选项“NSAllowsArbitraryLoads”的情况下,我已经解决了这个问题

                      解析服务器,因为任何 node.js 服务器都会提供一个您必须指定的 public https url。例如:

                      parse-server --appId --masterKey --publicServerURL https://your.public.url/some_nodejs

                      欢迎关注我的configuration files

                      【讨论】:

                        【解决方案18】:

                        我设法通过结合许多提到的选项来解决这个问题。我将附上一份清单,其中列出了我必须做的所有事情。

                        简而言之:

                        1. NSAllowsArbitraryLoads 设置为 true 以用于我的手表扩展程序(不是我的手表应用程序)。
                        2. 确保我使用的是 https 而不是 http

                        第一步:

                        首先,最明显的是,我必须在手表扩展的 info.plist 中添加一个 NSAppTransportSecurity 键作为字典,并将一个名为 NSAllowsArbitraryLoads 的子键作为布尔值设置为 true。 在手表扩展中设置此项,而不是在手表应用的 plist 中设置。虽然请注意,这允许所有连接并且可能不安全。

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

                        第二步:

                        然后我必须确保我尝试加载的网址是https 而不仅仅是http。对于我使用的仍然是 http 的任何网址:

                        斯威夫特

                        let newURLString = oldURLString.stringByReplacingOccurrencesOfString("http", withString: "https")

                        Obj-C:

                        NSString *newURLString = [oldURLString stringByReplacingOccurrencesOfString:@“http” withString:@“https”];

                        【讨论】:

                          【解决方案19】:

                          iOS 9(可能)强制开发者独占使用App Transport Security。我在某个地方无意中听到了这个,所以我不知道这是否是真的。但我怀疑它并得出了这个结论:

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

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

                          在应用程序的 info.plist 中,NSAppTransportSecurity [Dictionary] 需要有一个密钥 NSAllowsArbitraryLoads [Boolean] 设置为 YES 或 Meteor 需要使用 https 来为其 localhost server 很快。

                          【讨论】:

                            【解决方案20】:

                            在 Xcode 7.1 以后(swift 2.0)

                            【讨论】:

                              【解决方案21】:

                              我已解决为 plist 文件。

                              添加一个 NSAppTransportSecurity : 字典。

                              将名为“NSAllowsArbitraryLoads”的子项添加为布尔值:是

                              【讨论】:

                                【解决方案22】:

                                你只需要在你的 URL 中使用 HTTPS 而不是 HTTP 就可以了

                                【讨论】:

                                • 当然,除非您不能使用 HTTPS,因为 Apple 不喜欢自签名证书。你没有这个问题吗?
                                • 在我的情况下,自从我更改为 HTTPS 后,它工作得很好
                                • 但是您使用的是自签名证书还是 CA 签名证书?只是好奇。当然,这是一个单独的问题,但我想我会问。
                                • 我没有在服务器端工作,我不知道说实话:(
                                • 在某些服务器环境(共享主机)上设置 HTTPS 连接可能很困难。
                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 2015-12-15
                                • 2015-12-19
                                • 2017-06-10
                                • 1970-01-01
                                • 2015-12-28
                                • 2020-01-21
                                相关资源
                                最近更新 更多