Search Your Question

Different task available in NSURLSession

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 :


  1. default sessions: behaviour like NSURLConnection 
  2. ephemeral sessions: not cache any content to disk 
  3. 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: 

  1. data tasks: retrieve data to memory 
  2. download tasks: download file to disk 
  3. 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