Ans :
NSURLSession : The NSURLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn’t running or, in iOS, while your app is suspended.
It is replacement of NSURLConnection from iOS 7.
Types of URL Sessions :
NSURLSession : The NSURLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn’t running or, in iOS, while your app is suspended.
It is replacement of NSURLConnection from iOS 7.
Types of URL Sessions :
- default sessions: behaviour like NSURLConnection
- ephemeral sessions: not cache any content to disk
- download sessions: store the result in file and transferring data even when app is suspended, exits or crashes
Based on above sessions, developers can schedule three types of tasks:
- data tasks: retrieve data to memory
- download tasks: download file to disk
- upload tasks: uploading file from disk and receiving response as data in memory
func httpGet(request: NSURLRequest!, callback: (String, String?) -> Void) {
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if error != nil {
callback(“”, error.localizedDescription)
} else {
var result = NSString(data: data, encoding:
NSASCIIStringEncoding)!
callback(result, nil)
}
}
task.resume()
}
No comments:
Post a Comment
Thanks