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

在升级到Xcode 7.0和iOS 9.0后,App Transport Security策略导致无法加载HTTP资源。解决方法是在info.plist文件中添加特定键值,允许不安全的加载或切换到HTTPS。通过修改info.plist,添加`NSAppTransportSecurity`字典,并设置`NSAllowsArbitraryLoads`为YES,可以解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

I am facing the Problem when I have updated my Xcode to 7.0 or iOS 9.0. 将Xcode更新为7.0或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" “无法加载资源,因为App Transport Security策略要求使用安全连接”

Webservice Method: 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];

}

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. 对于Xcode早期版本和iOS先前版本,该服务运行良好,但是当我更新到iOS 9.0上的Xcode 7.0时,它开始给我类似我在调用上述Web服务方法时出现的问题。 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." 连接失败:错误Domain = NSURLErrorDomain代码= -1022“无法加载资源,因为App Transport安全策略需要使用安全连接。” 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.} UserInfo = {NSUnderlyingError = 0x7fada0f31880 {Error Domain = kCFErrorDomainCFNetwork代码= -1022“(null)”},NSErrorFailingURLStringKey = MyServiceURL ,NSErrorFailingURLKey = MyServiceURL ,NSLocalizedDescription =无法加载资源,因为应用传输安全策略需要使用安全的安全性连接。}

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 ios9无法加载资源
  2. App Transport Security Xcode 7 beta 6 App Transport Security Xcode 7 Beta 6
  3. https://stackoverflow.com/a/32609970 https://stackoverflow.com/a/32609970

#1楼

参考:https://stackoom.com/question/2CurQ/无法加载资源-因为-应用程序传输安全性-策略要求使用安全连接


#2楼

I have solved it with adding some key in info.plist. 我已经通过在info.plist中添加一些键解决了它。 The steps I followed are: 我遵循的步骤是:

  1. Opened my Project target's info.plist file 打开我的项目目标的info.plist文件

  2. Added a Key called NSAppTransportSecurity as a Dictionary . 添加了一个名为NSAppTransportSecurity的密钥作为Dictionary

  3. Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES as like following image. 添加了一个名为NSAllowsArbitraryLoads的子项作为Boolean ,并将其值设置为YES ,如下图所示。

在此处输入图片说明

Clean the Project and Now Everything is Running fine as like before. 清理项目,现在一切都像以前一样运行良好。

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

EDIT: OR In source code of info.plist file we can add that: 编辑:或在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>

#3楼

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


#4楼

iOS 9 (may) force developers to use App Transport Security exclusively. iOS 9(可能)迫使开发人员专门使用App Transport Security I overheard this somewhere randomly so I don't know whether this is true myself. 我在某个地方随机听到了这个消息,所以我自己也不知道这是不是真的。 But I suspect it and have come to this conclusion: 但我对此表示怀疑,并得出以下结论:

The app running on iOS 9 will (maybe) no longer connect to a Meteor server without SSL. 在iOS 9上运行的应用将(可能)不再连接到没有SSL的Meteor服务器。

This means running meteor run ios or meteor run ios-device will (probably?) no longer work. 这意味着运行流星运行ios或流星运行ios设备将(可能吗?)不再起作用。

In the app's info.plist, NSAppTransportSecurity [Dictionary] needs to have a key NSAllowsArbitraryLoads [Boolean] to be set to YES or Meteor needs to use https for its localhost server soon. 在应用程序的info.plist中, NSAppTransportSecurity [Dictionary]需要将密钥NSAllowsArbitraryLoads [Boolean]设置为YES否则Meteor很快将在其localhost server上使用https


#5楼

Transport security is provided in iOS 9.0 or later, and in OS X v10.11 and later. iOS 9.0或更高版本以及OS X v10.11和更高版本提供了传输安全性

So by default only https calls only allowed in apps. 因此,默认情况下,仅https调用仅在应用程序中允许。 To turn off App Transport Security add following lines in info.plist file... 要关闭App Transport Security,请在info.plist文件中添加以下行...

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

For more info: 有关更多信息:
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33 https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33


#6楼

I have solved as plist file. 我已经解决为plist文件。

Add a NSAppTransportSecurity : Dictionary. 添加一个NSAppTransportSecurity:字典。

Add Subkey named " NSAllowsArbitraryLoads " as Boolean : YES 将名为“ NSAllowsArbitraryLoads”的子项添加为布尔值:是

在此处输入图片说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值