首页 文章

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

提问于
浏览
424

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

“无法加载资源,因为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];

}

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

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

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

18 回答

  • 36

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

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

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

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

    enter image description here

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

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

    EDIT: 或者在 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>
    
  • 12

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

    enter image description here

    或者,作为源代码:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>domain.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    编辑后清理和构建项目 .

  • 2

    Transport security 在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

  • 9

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

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

    在Swift 4中你可以使用

    • 转到Info.plist

    • 单击信息属性列表的加号

    • 添加 App Transport Security Settings 作为字典

    • 单击加号图标 App Transport Security Settings

    • 添加 Allow Arbitrary Loads 设置 YES

    贝娄图像看起来像

    enter image description here

  • 237

    我已解决为plist文件 .

    添加NSAppTransportSecurity:Dictionary .

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

    enter image description here

  • 836

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

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

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

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

    enter image description here

  • 9

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

  • 0

    在Xcode 7.1之后(swift 2.0)

    enter image description here

  • 21

    From Apple documentation

    如果您正在开发新应用,则应该专门使用HTTPS . 如果您有现有应用,则应尽可能多地使用HTTPS,并创建一个计划,以便尽快迁移其余应用 . 此外,您通过更高级别的API进行的通信需要使用具有前向保密性的TLS 1.2版进行加密 . 如果您尝试 Build 不具有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

  • 10

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

    enter image description here

  • 2

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

    The app running on iOS 9 will (maybe) no longer connect to a Meteor server without SSL.

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

    在应用程序的info.plist中, NSAppTransportSecurity [Dictionary] 需要将密钥 NSAllowsArbitraryLoads [Boolean] 设置为 YES ,或者Meteor需要尽快使用 https 作为其 localhost server .

  • 29

    If you are using Xcode 8.0 to 8.3.3 and swift 2.2 to 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
    

    enter image description here

  • 9

    打开 pList.info 作为 Source Code ,在 </dict> 之前的底部添加以下代码,

    <!--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 . 谢谢 .

  • 3

    我设法通过结合许多提到的选项来解决这个问题 . 我将列出一份清单,列出我必须做的所有事情才能让它发挥作用 .

    简而言之:

    • 为我的 Watch 扩展程序(不是我的 Watch 应用程序)设置 NSAllowsArbitraryLoads 为true .

    • 确保我使用的是 https 而不是 http .


    Step one:

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

    enter image description here

    要么

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

    Step two:

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

    Swift

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

    Obj-C:

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

  • 6

    Make sure you change the right info.plist file .

    这是 second time 我在这个问题上浪费时间,因为我没有在MyProjectNameUITests下更改info.plist .

  • 22

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

    解析服务器作为任何node.js服务器呈现您必须指定的 public https url . 例如:

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

    随意看看我的configuration files

相关问题