Skip to content

Instantly share code, notes, and snippets.

@collinsrj
Created May 27, 2015 06:26
Show Gist options
  • Save collinsrj/38d4dc66308b3bfde168 to your computer and use it in GitHub Desktop.
Save collinsrj/38d4dc66308b3bfde168 to your computer and use it in GitHub Desktop.
Looking at NSError Handling from NSURLSession
import UIKit
import XCPlayground
// HTTPS Request
var secureSession = NSURLSession.sharedSession()
if let url = NSURL(scheme: "https", host: "api.github.com", path: "/users/collinsrj/repos") {
//if let url = NSURL(scheme: "https", host: "goo.gl", path: "/WsXbKr") {
var httpsTask = secureSession.dataTaskWithURL(url) {
(data, response, error) -> Void in
if error != nil {
let err = error!
// handle the error
println("Code: \(err.code)")
println("Domain: \(err.domain)")
println("UserInfo: \(err.userInfo)")
println("Description: \(err.localizedDescription)")
println("RecoveryOptions: \(err.localizedRecoveryOptions)")
println("RecoverySuggestion: \(err.localizedRecoverySuggestion)")
println("FailureReason: \(err.localizedFailureReason)")
println("HelpAnchor: \(err.helpAnchor)")
} else if let httpResponse = response as? NSHTTPURLResponse {
switch httpResponse.statusCode {
case 200:
let stringData = NSString(data: data, encoding: NSUTF8StringEncoding)
default:
println("An unexpected http response: \(NSHTTPURLResponse.localizedStringForStatusCode(httpResponse.statusCode))")
}
} else {
println("Something else happened.")
}
}
httpsTask.resume()
}
XCPSetExecutionShouldContinueIndefinitely()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment