NSURLSession not working in Xcode 7 the same code works in Xcode6

Hi All,


I have having some issue with using NSURLSession in Xcode7 and Swift2. For some reason I keep getting NSURLErrordomain error but the same code

is working on Xcode6 with swift 1.2.


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()


https://drive.google.com/file/d/0B-bZj242s5iAX2Z0bzVETjE3akk/view?usp=sharing


Please let me know if I am missing something here.

I suspect you're being affected by App Transport Security. To learn more watch:

Also test on beta 2 and set up your Info.plist based on the documentation in App Transport Security Technote.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1@apple.com"

the documentation https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html#//apple_ref/doc/uid/TP40016240 describes wrong structure of the keys:


<key>NSAppTransportSecurity</key>

<dict>

<key>NSExceptionDomains</key>

<dict>

<key>NSAllowsArbitraryLoads</key>

<true/>

</dict>

</dict>


The correct one that works is:


<key>NSAppTransportSecurity</key>

<dict>

<key>NSAllowsArbitraryLoads</key>

<true/>

<key>NSExceptionDomains</key>

<dict/>

</dict>

I'm having a similar issue when trying to download a file via URL in Xcode Beta 5 and Swift 2 running iOS9


Would really appreciate any help as I am going crazy trying to find out what it doesn't like about the code in swift2 ? The documentation says the method signature has not changed. I click on the code suggestion to get the completion handlers then it gives me errors. I took the code back into Swift 1.2 in Xcode 6.4 and it ran straight away 😕


This code runs fine in iOS 8.4 Xcode 6.4


let messageURL = NSURL(string: "http:/
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL!,
            completionHandler: {
                (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
     
                var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
     
                println("response : \(response)")
                println(urlContents)
     
        })
        downloadTask.resume()


I get a response and urlContents printed.


The same code for iOS9 gives me a compile error complaining about the method signature of the downloadTaskWithURL function


        let messageURL = NSURL(string: "http:/
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL!,
            completionHandler: {
                (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
       
                var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
       
                print("response : \(response)")
                print(urlContents)
       
        })
        downloadTask.resume()


The compile error is shown below:


https://drive.google.com/file/d/0B7a9zgAH2mBfeHM0TjE5MF91RzA/view?usp=sharing

http://i.imgur.com/AW9ge7R.png

Successful code in iOS8.4:

http://i.imgur.com/P17JROM.png

NSURLSession not working in Xcode 7 the same code works in Xcode6
 
 
Q