Created
July 16, 2016 10:58
-
-
Save 29satnam/184818411744a0ef87fc480d0075a17d to your computer and use it in GitHub Desktop.
Swift HTTP GET Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let session = NSURLSession.sharedSession() | |
let url = NSURL(string: "http://someurl.com") | |
let request = NSMutableURLRequest(URL: url!) | |
request.HTTPMethod = "GET" | |
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in | |
if (data != nil) { | |
let res = response as! NSHTTPURLResponse!; | |
if 200..<300 ~= res.statusCode { | |
do { | |
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary | |
// parsing resonse | |
let username:String = jsonData.valueForKey("username") as! String | |
print(username) | |
} catch _ as NSError { | |
} | |
} | |
} | |
}) | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment