Before the Swift 2.0 Update this code worked perfectly to download my JSON File from the Server with a PHP Script:
let url = NSURL(string: webAdress)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 5.0)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
After the Update Xcode asked me to do some changes. I did and the code had no Error, but it always throws...
let url = NSURL(string: webAdress)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
let request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 5.0)
var response: NSURLResponse? = nil
var reply = NSData()
do {
reply = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
} catch {
print("ERROR")
}
Looking forward to your solutions!
Here's an example using the new NSURLSession - apparently NSURLConnection has been deprecated in iOS 9.
I think it's super clean, there's just not much documentation on it. Let me know if you have any trouble getting this to work.