NSURLSession in Swift: get and post data

In this new Swift 2 tutorial you'll learn how to use the NSURLSession Swift function. You'll see different examples that illustrate how to define the different NSURLSession methods in various scenario, for downloading data or sending post requests.

Let's start this new tutorial by creating a new Swift iOS single view application.

NSURLSession in Swift: get and post data

For this tutorial we'll use iOS 9 as a deployment target and the examples are based on a server side script to get the data located on the server that does not have the https, so, for  this reason, we have to edit the info.plist file, in order to allow this domain.

Edit the Info.plist to allow an http domain

Right click on the info.plist file, select open as, Source code. Add the lines of code that allow the http connection to this server.

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>www.kaleidosblog.com</key>
            <dict>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>


NSURLSession in Swift: get and post data

The NSURLSession is an asynchronous function and has three main principal methods: 

  • dataTaskWithUrl - dataTaskWithRequest
  • downloadTaskWithUrl - downloadTaskWithRequest
  • uploadTaskWithUrl - uploadTaskWithRequest

The dataTaskWithUrl is for normal data requests (when for example you'll have to ask to a web server for some data).
The downloadTaskWithUrl is for downloading files. It can resume the downloads for you and, as soon as the data is ready you'll have the temporary path in which the data is stored.
The uploadTaskWithUrl is for uploading data.

Server definition

The server in this example receives the data through a post request and simply replies it back. The post parameter has this format:

Post Request : 'data' > String

In this way we'll see if everythings works fine.
The url for the requests is located at this address:
 http://www.kaleidosblog.com/tutorial/nsurlsession_tutorial.php

In swift I've defined a constant variable with this url, called url_to_request.

NSURLSession: dataTaskWithUrl example

Let's start this example  with the dataTaskWithUrl function.

    func data_request()
    {
        let url:NSURL = NSURL(string: url_to_request)!
        let session = NSURLSession.sharedSession()
        
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
        
        let paramString = "data=Hello"
        request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
        
        let task = session.dataTaskWithRequest(request) {
        (
            let data, let response, let error) in
            
            guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
                print("error")
                return
            }
            
            let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print(dataString)
            
        }
        
        task.resume()
        
    }

The difference between dataTaskWithUrl and dataTaskWithRequest, as the name suggests, is that dataTaskWithUrl requires an URL as input, while the dataTaskWithRequest requires a NSMutableURLRequest variable. The difference between the URL and the NSMutableURLRequest is that the URL has to been initialized first with your requirments and nothing can change then, while with the NSMutableURLRequest you can define the variable and then you can set the behaviour like the cache policy, timeout, or the http method used. In  this example the cache has been disabled in order to reload from the original source the request. The guard method let us check the response data.

NSURLSession: downloadTaskWithUrl example

  func download_request()
    {
        let url:NSURL = NSURL(string: url_to_request)!
        let session = NSURLSession.sharedSession()
        
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
        
        let paramString = "data=Hello"
        request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
        
        
        let task = session.downloadTaskWithRequest(request) {
            (
            let location, let response, let error) in
            
            guard let _:NSURL = location, let _:NSURLResponse = response  where error == nil else {
                print("error")
                return
            }
            
            let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
            
            guard let _:NSString = urlContents else {
                print("error")
                return
            }
            
            print(urlContents)
            
        }
        
        task.resume()
        
    }


The downloadTaskWithUrl stores in a temporary file the data you've received from the server. With the location variable you can access directly to this temporary file, or get the data from this file. This method is able to resume a suspended download.

NSURLSession: uploadTaskWithUrl example

  func upload_request()
    {
        let url:NSURL = NSURL(string: url_to_request)!
        let session = NSURLSession.sharedSession()
        
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
    
        
        let data = "data=Hi".dataUsingEncoding(NSUTF8StringEncoding)

        
        let task = session.uploadTaskWithRequest(request, fromData: data, completionHandler:
            {(data,response,error) in
            
                guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {
                    print("error")
                    return
                }
                
                let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print(dataString)
            }
        );
        
        task.resume()
        
    }


This method is able to upload data to a server, by directly putting the data inside the formData variable.

Download the example
UPDATE: Download the swift 3 project example


Comments 58

Comments are closed: this is a read-only archive.

  1. sohan
    Hi,

    Can I get to see nsurlsession_tutorial.php file,
    I want to c how the php code is written..
    Also
    how can load my tableview from php file
    1. Andrea Author
      Hi, thank you for your comment. It's very simple, here is the link: http://www.kaleidosblog.com/tutorial/nsurlsession_tutorial.txt . If you have more data to send (array, objects), yoy have to encode the array in a string using the php json_encode function. In this tutorial you'll see the table parsing from the json format http://www.kaleidosblog.com/swift-uitableview-load-data-from-json
  2. Chris
    I love the code, but I'm trying to work out how I can put it into a separate file so I don't have multiple lines of the same code.

    I am trying to do it with a completion handler but not having much luck getting it to work.

    Any help or a tutorial on it would be great. :)
    1. Andrea Author
      You've to define a protocol for your completion handler. In this protocol you'll define the function that will be called as soon as the data is arrived. When the function for downloading the data is called, you also have to pass the class that will be called (the class that implement the function of the protocol). Hope it helps, but in the next days I'll write a tutorial about that :-)
    2. Chris
      Thanks for the reply, I kind of know what you're saying but still a little confused. I'll have another look at it tomorrow when I have more time.


      Hopefully you'll get time to do a tutorial on it and that might help a little more but if you don't then it doesn't matter. Either way thanks for the tutorials already here :)
    3. Chris
      Wow! Didn't expect that so quickly.. Thank you very much i'll check it out :)
  3. Kody
    this is awesome!
  4. HappyMan
    Thank you for such detailed teaching!!
  5. Pawan Shahani
    Thanks a lot for this tutorial & appreciate your pains for the below tutorial also,
    http://www.kaleidosblog.com/protocol-delegate-in-swift-how-to-define-and-use-a-protocol-function-to-handle-external-events

    I was struggling for these points.
    1. Andrea Author
      You are welcome!
  6. Fabrizio
    Fantastico!
    Finalmente ho trovato ciò che cercavo!
    Se con la funzione data_request() io volessi inviare un array oppure un Json, cosa dovrei modificare?
    Spero tanto in una tua risposta.
    Grazie mille
    1. Andrea Author
      Ciao, grazie per il tuo commento. Nella funzione data_request dovresti inserire i parametri oppure la stringa contenente il json, ad esempio in questo modo data_request(String: dati). Quindi puoi utilizzare la variabile paramString per concatenare i tuoi parametri o la stringa in json. In questo articolo puoi trovare un esempio più concreto: http://www.kaleidosblog.com/how-to-parse-and-extract-json-data-in-swift
    2. Fabrizio
      Grazie mille, mi studio il tutorial!
      Ma c'è un limite di grandezza della stringa che inviamo? Grazie di nuovo
    3. Andrea Author
      Non c'è un limite di lunghezza, c'è però un limite sulla dimensione della richiesta post che fai. Questo limite può comunque essere modificato dalle impostazioni del server. Ma finchè invii solo array di testo in formato json, senza inserirci immagini all'interno, difficilmente lo raggiungi.
    4. Fabrizio
      Grazie mille di nuovo!
  7. Nikita
    Hey,
    Nice Article. Learnt a lot from this.

    Have some doubts:
    1. Shouldn't the downloadTask request.HTTPMethod be equal to Get, instead of Post?
    2. Why is the dataTask Post?
    3. I think you forgot to set the body of the request in UploadTask.
    1. Andrea Author
      Hi, thank you for your comment. The requests with the NSURLSession can be both Post or Get requests. In this example, I've prepared a php script that allows only post requests. In all the cases the same php script is called, and in all cases the post parameter are properly sent.
    2. Nikita
      I appreciate the quick reply. Thank you for explaining. Just wanted to clarify on the third point again. you have not set the HTTPBody of the request in Upload Task.
  8. venkatesh
    HI if i send request to server which way i can fallow
    i mean code and how to send login details to server
    and get how to get some data please tell me........
    1. Andrea Author
      Hi, thank you for your comment. You can use the dataTaskWithUrl function, send the data using a post request to your server (better to use https when sending sensible information) with the login information, then your server can respond with the data in json format. Hope it helps, if you need a more concrete example let me know.
  9. SAUD
    Amazing Tutorial, Thank you.
  10. tatiana
    great tutorial
  11. Ali Ghaddar
    thank you for the tutorial. i faced a problem when building the app. it said that "Value of type 'NSURLSession' has no member 'dataTaskWithRequest'
    1. Andrea Author
      Hi, thank you for your comment. Have you implemented the NSurlsession function inside a new swift file? If yes, be sure to import all the required libraries at the top of the file (import UIKIt..). Otherwise, it is better if I can see your project to help you a bit more.
  12. Ramesh Chandran A
    Nice tutorial. Simple illustration. Easy to understand.
  13. Patrick
    Hey, great tutorial! I am a little confused about the idea though. Is it possible to do something like the user clicks a button on your app and it posts a message to your twitter/facebook or something along those lines?
    1. Andrea Author
      Hi, thank you for your comment. You can for sure add a button that once clicked perform some data post request. However for facebook or twitter you need to use their api in order to let the user login, add access to your app and post messages. If you need I can write a tutorial about it! Just let me know
  14. Rob Dingwall
    Thank you for this very clear and usable example.
  15. Scott
    I'm a little confused about something. The code and explanation seem very straight forward, but when I run the data_request() function as copied from this page (url_to_reuquest replace with variable) the program never executes the code in the body under the line let task = session.dataTaskWithRequest(request) {

    Any idea what I'm doing wrong?
    1. Andrea Author
      Hi, can you please send me your project to let me help you?
  16. Kris
    Hi, my question is similar to Venkatesh.

    "HI if i send request to server which way i can fallow i mean code and how to send login details to server and get how to get some data please tell me........"

    Will you be able to give me some concrete example.

    I've used dataTaskWithUrl with my api link created in Flask.

    It return:
    Optional({
    "success": false
    }
    )


    Seems like I did not login successfully. I don't know what have I missed out.
    1. Andrea Author
      Hi, in the next days I will write a tutorial about the login page with username and password and how to send the login details to a server in order to perform the login and keep the session active!
  17. swaaws
    hey in swift 3 are some changes:

    func data_request(){
    let url:NSURL = NSURL(string: "http://example.com/")!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url as URL)
    //request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    //request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.httpMethod = "POST"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

    let paramString = "data=Hello"
    request.httpBody = paramString.data(using: String.Encoding.utf8)

    let task = session.dataTask(with: request as URLRequest) {
    (
    data, response, error) in
    // No idea
    // guard let _:NSData = data, let _:NSURLResponse = response , error == nil else {
    // print("error")
    // return
    // }

    let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print(dataString)

    }

    task.resume()

    }
  18. David
    Hi Andrea, thanks for the wonderful tutorial.

    one question regarding "request".
    In case of GET method, is it possible to skip "request" part?

    let task = session.dataTask(with:" url "- instead of "request") ....

    seems to work. Will it be a problem somewhere else?
    1. Andrea Author
      Hi, thank you for your comment, the difference is that you cannot customize your request, by adding a post parameter, header information, define the cache policy, the timeout and so on. It will just use the default parameter, but the request will work fine too!
    2. David
      Hi Andrea,

      Thank you for your answer.
  19. Pat
    Would this work if the website we are communicating with is HTTPS? normally your browser does the SSL "handshake" with the HTTPS site and everything is secure.. is the iPhone capable of doing this? So My question is 2 parts, 1) will this code and data transfer work with a website that is https? and 2) if it does work, is the data actually secured (encrypted)? Thank you very much
    1. Andrea Author
      Hi, yes https is supported by NSURLSession, just write the link as https and everything is done by nsurlsession. Yes with the above code you can use http or https as you want, just write the link. Sure when you use https the data is always encrypted.
  20. Jorge
    Thank you for great tutorial. Could you also post the content of the php - file?
    1. Andrea Author
      Hi! Thank you for your comment. Sure, it is very simple, you can find it here: http://www.kaleidosblog.com/tutorial/nsurlsession_tutorial.txt
  21. JohnnyDarko
    Hi everybody! As you know there is a lot of great ways to make some money online. If you are involved with Binary Options, or maybe even if you are new to the world of Binary Options and would like to learn a little more about it and how it is such a great way to be earning more money on the side in your free time , then you should think of buying AutoBinarySignals.
    [url=https://youtu.be/YNDg6j-ntvs]AutoBinarySignals[/url] is an automated software solution which will help you trading and making money with Binary Options. This system will be trading Binary Options for you. It is simply a ?set-it-and-forget-it? system which will work on autopilot.
    [url=http://www.facebook.com/autobinarysignals101]AutoBinarySignals[/url] will only take a few moments of your time to set up and get running, and it is simple to learn. There are no confusing charts, baffling analysis or complex methods to learn. This software will work for you once it has been set up.
  22. JohnnyDarko
    Hello everyone! As you may know there is a lot of great ways to earn some money online. If you are involved with Binary Options, or maybe even if you are new to the world of Binary Options and want to learn more about it and how it is a great way to be earning some money on the side in your free time , then you should think about getting AutoBinarySignals.
    [url=https://youtu.be/YNDg6j-ntvs]AutoBinarySignals[/url] is an automated solution which will help you in trading and earning money with Binary Options. This software will be trading Binary Options for you. It is simply a ?set-it-and-forget-it? software system which will be working for you on autopilot.
    [url=http://www.facebook.com/autobinarysignals101]AutoBinarySignals[/url] will only take a few moments of your time to set up, and it is very simple to learn. There are no confusing charts, confusing analysis or complex methods to learn. This software will work for you once it is set up.
  23. mahbod
    Hi thanks for tutorial
    please help me how can I fix this problem.
    I create Registration page and I make
    providedEmailAddress for userEmail, userPassword, UserRepeatPassword that user have to complete these fields for access application.
    I am very new in swift and do I have to use "POST" or "GET"???
    where can I define my parameters in your code?

    for example I don't understand this line of your code:

    let paramString = "data=Hello" what is this line code??
    1. Andrea Author
      Hi, thank you for your comment. For login or registration purpose you can use the post request method. The parameters have to been written into the body request in the form of key=value&key2=value2.. In the paramString part you define the parameter to send with the post request. So you have to concatenate all the keys and values that you have to send in this part!
  24. JohnnyDarko
    Hey folks! As you know there are a lot of great ways to make money online. If you are involved with Binary Options, or maybe even if you are new to the world of Binary Options and would like to learn more about it and how it is such a great way to be earning more money on the side in your free time , then you should think of buying AutoBinarySignals.
    AutoBinarySignals is an automated solution which will help you in trading and earning money with Binary Options. This system will trade Binary Options for you. It is simply just a ?set-it-and-forget-it? software system which will be working for you on autopilot.
    AutoBinarySignals will only take a few minutes of your time to set up, and it is easy to learn. There are no confusing charts, baffling analysis or complex methods to learn. The software will work for you once it is set up and running.
  25. sbobe
    Thanks for review
    sbobe
    1. Ton
      I got this warning with iOS 10.3
  26. Ton
    I get this warning in line 79 (see sample) and don't know how to solve it.
    Can anybody give me a hand? Thanks!

    guard let _:NSString = urlContents else {
    print("error")
    return
    }

    nsurlsession_tutorial_swift_3/nsurlsession_tutorial/ViewController.swift:79:19: Non-optional expression of type 'NSString' used in a check for optionals
    1. Ton
      Solved it!!

      let urlContents = try? NSString(contentsOf: location!, encoding: String.Encoding.utf8.rawValue)


      guard let _:NSString = urlContents else {
      print("error")
      return
      }

      print(urlContents!)
    2. Ton
      I got this warning with iOS 10.3
  27. Bibin
    I need to post multiple methods........
    e.g.: &name=bibin&lastname=kj¬e=My firstname is bibin & lastname is kj.

    in my case "& last name is kj" is not saved. how can I solve this...?

    email:[email]
  28. Sunil
    Post Service Issue:
    URL :http://appios.soolegal.com/api/LGLState
    Parameter :country_Id=100
    Get error : 500 (Server error)
    Lang: Swift by using AFNetworking 3.0

    But It's working by using this URL.
    https://www.hurl.it

    Please check it once. I am stuck very serious problem.

    Thanks
    Sunil
  29. babak
    Hi,
    I need send mysql sql query for show in uitableview
    like below:
    select * from table1
    Can I use NSURLSession and How?
  30. abhilasha rathore
    hey
    i am making a very simple application in ios by using xcode 8 and swift 3. i want to fetch the data with both methods post and get and after that i have to save in my application tem or permanent. i have seen many videos bt still confused and getting so many errors . can u pls tel me the process in easy way. i am begginer and adopted ios application developer as a carrier.
    thanku
  31. Stremove.com
    URLSession is the class responsible for making HTTP request and getting HTTP response. In a really simplified analogy, think of URLSession as a web browser and it can have multiple tabs opening multiple website. Those tabs which request and load website data are URLSessionTask. One URLSession can have multiple URLSessionTask send request to different website.
  32. annadZef
    [url=https://misternews.ru/sliding-patio-door-lock-cylinders-sliding-door-handle-repair/]Sliding Patio Door Lock Cylinders: Sliding Door Handle Repair[/url]
  33. GabrielDAB
    http://dixckxx.xyz - pantyhose
    http://analbigdick.xyz - bisexual
    http://vaginals.xyz - spank
    http://vulvas.xyz - breast
    http://xxvoyeur.xyz - boob
    http://fistxxx.xyz - bisex
    http://hentai-x.xyz - butt fucking
    http://boobed.xyz - deepthroat
    http://cum-xxx.xyz - pornostar
    http://orgazm.xyz - escort