Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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
like image 248
Manab Kumar Mal Avatar asked Sep 17 '15 13:09

Manab Kumar Mal


People also ask

How do I add an app to the Transport Security Exception?

If your app still needs to make insecure connections to specific domains, you can configure ATS exceptions for just those domains. Add Exception Domains directly to your app's Info. plist or in the project editor. Navigate to “Signing & Capabilities” and choose the “+ Capability” option.

What is App transport security?

App Transport Security (ATS) is an iOS feature that forces mobile apps to connect to back-end servers using HTTPS, instead of HTTP, to encrypt data in transit. ATS enforces a minimum security level for communications between a mobile app and web services that support its functionality.

What is NSAllowsArbitraryLoadsInWebContent?

NSAllowsArbitraryLoadsInWebContent. A Boolean value indicating whether all App Transport Security restrictions are disabled for requests made from web views.


2 Answers

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.

enter image description here

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> 
like image 84
Manab Kumar Mal Avatar answered Oct 04 '22 23:10

Manab Kumar Mal


Be aware, using NSAllowsArbitraryLoads = true in the project's info.plist allows all connection to any server to be insecure. If you want to make sure only a specific domain is accessible through an insecure connection, try this:

enter image description here

Or, as source code:

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

Clean & Build project after editing.

like image 35
Arjan Avatar answered Oct 04 '22 23:10

Arjan