Strange (bug?) With Xcode 7 / iOS 9 b5 with dataWithContentsOfURL

I have a piece of code that works as expected on all versions of iOS, but not on iOS 9:

NSData *response = [NSData dataWithContentsOfURL: [NSURL URLWithString: url] options:NSDataReadingUncached error:&error]; 

This is plain json text.

I got this error:

Domain Error = NSCocoaErrorDomain Code = 256 "File" xxx.php "could not be opened." UserInfo = {NSURL = http://xxx.xxx.com/xxx/xxx.php?lang=fr }

How can this url be indexed as a file? Response = nil ...

Thanks.

+7
ios ios9 xcode7
source share
2 answers

Technically, this is due to changes to NSURLSession on the network in iOS9. To fix your problem, you need to go to the info.plist application, NSAppTransportSecurity [Dictionary] must have the NSAllowsArbitraryLoads [Boolean] key to set YES or call URLs using https.

You can learn more about NSURLSession changes on the iOS9 network at http://devstreaming.apple.com/videos/wwdc/2015/711y6zlz0ll/711/711_networking_with_nsurlsession.pdf?dl=1

+10
source share

After debugging for 3 hours, I avoided the error all together using the asynchronous NSMutableURLRequest, which, as I noticed, is much faster than synchronous NSData.

 let requestURL: NSURL = NSURL(string: url)! let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(urlRequest) { (data, response, error) -> Void in if error == nil { var response = UIImage(data:data!) } else { NSLog("Fail") } } task.resume() 
0
source share

All Articles