天天看點

無法加載資源,因為“應用程式傳輸安全性”政策要求使用安全連接配接

本文翻譯自: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”的子項添加為布爾值:是
無法加載資源,因為“應用程式傳輸安全性”政策要求使用安全連接配接