NSURLSession changes in iOS9

June 18, 2015
Shrikar Archak

<Image alt="Apache Spark" objectFit="contain" src="/static/images/NSURLSessions.png" height={250} width={1000} placeholder="blur" quality={100} />

I am sure anyone who started with iOS app development would have dealt with NSURLSession. In this post I wanted to tell you about the changes to NSURLSession in iOS9 for security reason. Lets take a look at this sample code. The code used to work perfectly fine in iOS8 but in iOS9 the code breaks and throws NSURLDomainError or CFNetwork SSLHandshake failed (-9824)

  let baseURL = NSURL(string: "https://itunes.apple.com/search?term=one%20republic")
  let downloadTask = session.downloadTaskWithURL(baseURL!, completionHandler: { (location, response, error) -> Void in
      if(error == nil){
          let objectData = NSData(contentsOfURL: location!)
          let tmpData :NSString = NSString(data: objectData!, encoding: NSUTF8StringEncoding)!
          print("success")
      } else {
          print("Failed")
      }

  })
  downloadTask!.resume()

This is because of the new security features in NSURLSession in iOS9. Take a look at this video for more information [https://developer.apple.com/videos/wwdc/2015/?id=711](http://Networking with NSURLSession)

You can fix this code by adding the NSAppTransportSecurity to the Info.plist with setting NSAllowsArbitraryLoads to true. Don't do this unless you know what you are doing and why you are doing it.

<Image alt="NSURLSession" objectFit="contain" src="/static/images/Screen-Shot-2015-06-18-at-8.24.19-AM-1024x577.png" height={250} width={1000} placeholder="blur" quality={100} />

NSAppTransportSecurity

  NSExceptionDomains

    yourserver.com


      NSIncludesSubdomains


      NSTemporaryExceptionAllowsInsecureHTTPLoads


      NSTemporaryExceptionMinimumTLSVersion
      TLSv1.1


 

For some reason I am not able to fetch the data from itunes without adding the keys to the Info.plist. Am I missing something?

Subscribe to the newsletter

Get notified when new content or topic is released.

You won't receive any spam! ✌️