无法加载资源,因为应用传输安全策略要求使用安全连接 [英] The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

查看:40
本文介绍了无法加载资源,因为应用传输安全策略要求使用安全连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 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);//配置请求NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];[请求 setValue:[NSString stringWithFormat:@"%@=%@", strSessName, strSessVal] forHTTPHeaderField:@"Cookie"];request.HTTPBody = [DataString dataUsingEncoding:NSUTF8StringEncoding];request.HTTPMethod = @"Post";//发布请求并处理响应NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){//处理响应如果(错误){NSLog(@"%@",[NSString stringWithFormat:@"连接失败:%@", [错误描述]]);//更新视图dispatch_async(dispatch_get_main_queue(),^{//隐藏加载器[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] delegate].window animation:YES];});返回;}NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];for (NSHTTPCookie * cookie 中的 cookie){NSLog(@"%@=%@", cookie.name, cookie.value);strSessName=cookie.name;strSessVal=cookie.value;}NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];}];[postDataTask简历];}

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

<块引用>

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

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

  1. 清理项目,现在一切都像以前一样正常运行.

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

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

    NSAppTransportSecurity<字典><key>NSAllowsArbitraryLoads</key><真/><key>NSExceptionDomains</key><字典><key>yourdomain.com</key><字典><key>NSIncludesSubdomains</key><真/><key>NSThirdPartyExceptionRequiresForwardSecrecy</key><假/></dict></dict></dict>

    I am facing the Problem when I have updated my Xcode to 7.0 or iOS 9.0. Somehow it started giving me the Titled error

    "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection"

    Webservice Method:

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

    The service is Running fine for Xcode earlier versions and iOS previous versions But when I have updated to Xcode 7.0 that is on iOS 9.0, it started to give me the Problem like following when I am calling the above web service method. The Logged Error which I am getting is:

    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.}

    I have tried Following Questions and answers but did not get any result there, is there any advance idea how I can remove that service call error?

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

    解决方案

    I have solved it with adding some key in info.plist. The steps I followed are:

    1. Opened my Project target's info.plist file

    2. Added a Key called NSAppTransportSecurity as a Dictionary.

    3. Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES as like following image.

    Clean the Project and Now Everything is Running fine as like before.

    Ref Link: https://stackoverflow.com/a/32609970

    EDIT: OR In source code of info.plist file we can add that:

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

    这篇关于无法加载资源,因为应用传输安全策略要求使用安全连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆