The resource could not be loaded because the Application Transport Security policy requires a secure connection
Having trouble updating Xcode to 7.0 or iOS 9.0. Somehow it started giving me the title error
"The resource could not be loaded because the application transport security policy requires the use of a secure connection"
Web service 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 was working fine for Xcode earlier versions and iOS earlier versions, but when I updated to Xcode 7.0 on iOS 9.0, it started to have issues like below when I call the above web service method. The logging error I get is:
Connection failed: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the application 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 application transport security policy requires the use of a secure security connection. }
I have tried the following questions and answers but got no results, are there any advanced ideas to remove this service call error?
I have solved it by adding some keys in info.plist. The steps I followed are:
Open my project target's
info.plist
fileAdded a named key
NSAppTransportSecurity
.Dictionary
Added a subkey called
NSAllowsArbitraryLoads
asBoolean
and set its valueYES
as shown in the image below.
Cleaned the project and now everything is running fine as before.
Reference link : https://stackoverflow.com/a/32609970
Edit: or in the source code of the info.plist
file , we can add:
<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>