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

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

问题描述

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


由于App Transport Security
,无法加载资源政策要求使用安全连接


Webservice方法:

   - (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);
//配置Request
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 animated:YES];


});
返回;
}
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
资源可以因为应用传输安全策略
需要使用安全连接而无法加载。
UserInfo = {NSUnderlyingError = 0x7fada0f31880 {错误
Domain = kCFErrorDomainCFNetwork Code = -1022(null)},
NSErrorFailingURLStringKey = MyServiceURL
NSErrorFailingURLKey = MyServiceURL
NSLocalizedDescription =无法加载资源,因为
应用传输安全策略要求使用安全的
连接。}


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




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



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



    编辑:
    OR在源代码 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>


    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>
    

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

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