Forked from kristopherjohnson/NSURLSession_Example.swift
Created
November 10, 2016 02:19
-
-
Save ggthedev/c14ce4732429af71c9134b557ab64ca2 to your computer and use it in GitHub Desktop.
Swift Playground using NSURLSession
This file contains hidden or 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
| import Foundation | |
| import XCPlayground | |
| // Let asynchronous code run | |
| XCPSetExecutionShouldContinueIndefinitely() | |
| if let url = NSURL(string: "http://www.google.com/") { | |
| let session = NSURLSession.sharedSession() | |
| let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in | |
| if error != nil { | |
| println("error: \(error.localizedDescription): \(error.userInfo)") | |
| } | |
| else if data != nil { | |
| if let str = NSString(data: data, encoding: NSUTF8StringEncoding) { | |
| println("Received data:\n\(str)") | |
| } | |
| else { | |
| println("unable to convert data to text") | |
| } | |
| } | |
| }) | |
| task.resume() | |
| } | |
| else { | |
| println("Unable to create NSURL") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment