NSURLSession not working as expected in watchOS 2 beta

When I load an url like this:

NSURL *downloadURL = [NSURL URLWithString:@"http://www.apple.com"];

NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];

self.task = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {

// more code

}];

[self.task resume];


The value of the error argument in the callback block is always like:

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7a77f6e0 {NSErrorFailingURLStringKey=https://www.apple.com/, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://www.apple.com/, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9824}


It seems even I requested the http resource, it always hit the https resource instead.

I did have capture the network traffic to see what is alctually going on, and I notice that every time I initiate a GET request to http://www.apple.com, it just internally do a "CONNECT www.apple.com:443" proxy request.


Has anyone ever run into this issue?

This is a new "security" feature. iOS 9 enforces secure connections, so all normal "http" connections will fail by default. Right now this won't work for many use cases, so you can opt-out for specific domains or even completely. Look for the keyword "App Transport Security" (ATS).

You'll find some more information in this thread:

https://forums.developer.apple.com/message/5857

iOS 9 and, by extension, watchOS 2 requires TLSv1.2 SSL for all hosts you plan to request data from. If it's 1.1 or lower, you need to specify exception domains in your app's Info.plist file.

For futher info on how to do that see elementarteilchen's suggestion and:

https://forums.developer.apple.com/message/7383#7383

https://forums.developer.apple.com/message/9055#9055

NSURLSession not working as expected in watchOS 2 beta
 
 
Q