0

I'm using XCode 7.3. Here is my code:

func postToServerFunction() {
    let url: NSURL = NSURL(string: "http://mytesturl.com")!
    let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    //let textfeld = String(UserTextField.text)
    let bodyData = "data=" + UserTextField.text!
    request.HTTPMethod = "POST"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
    {
        (response, data, error) in}}

This works fine and my php-Script gets the string it should. But there is the sendAsynchronousRequest was deprecated in iOS 9 message. As I read, with Swift 2 the method has changed. I searched a lot for this error, but I'm not able to convert the code that it matches to Swift 2. As I also read, i should use something like this

let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request)

but I can't get it down. And actually, I don't understand every single line. I wrote the code myself, but from some examples that it works for me.

kb920
  • 3,039
  • 2
  • 33
  • 44
plategt
  • 35
  • 2
  • 11
  • 1
    As I worte, I know the post you linked already. But I'm not able to convert it with this example. I don't know what I have to replace and witch line not. Maybe somebody can explain it a bit. Thanks. – plategt Apr 13 '16 at 08:14
  • Instead of setting up the NSURLSession yourself, I'd rather use Alamofire (https://github.com/Alamofire/Alamofire) which is way more elegant and easy to use.. – t0day Apr 13 '16 at 08:16
  • use [Alamofire](https://github.com/Alamofire/Alamofire) as @t0day said. – Ketan Parmar Apr 13 '16 at 08:19

2 Answers2

2

This is the most basic way you'd use a shared session:

if let url = NSURL(string: "http://mytesturl.com"),
       userField = UserTextField.text {

    let request = NSMutableURLRequest(URL: url)
    let bodyData = "data=" + userField
    request.HTTPMethod = "POST"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)

    let session = NSURLSession.sharedSession()

    let dataTask = session.dataTaskWithRequest(request,completionHandler: {(data,response,error) in


        }
    )

    dataTask.resume()
}

For more detail on the other approaches including background and ephemeral sessions, as well as how to handle the NUSRLResponse, see my blogpost.

sketchyTech
  • 5,746
  • 1
  • 33
  • 56
0

As you've found out NSURLConnection has been deprecated and NSURLSession is the new shiny.

For your example to work you still need to use the NSURL and the NSURLRequest that you have already created, and then you need an NSURLSession which you can use in various ways.

I can see that you already use the callback so in your case I assume it would be

session.dataTaskWithRequest(request) { (data, response, error) in
    //magic goes here
}

and then the important thing to remember is to call resume() on your task.

So...to translate your code I'm thinking something along those lines

func postToServerFunction() {
    let url: NSURL = NSURL(string: "http://mytesturl.com")!
    let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    //let textfeld = String(UserTextField.text)
    let bodyData = "data=" + UserTextField.text!
    request.HTTPMethod = "POST"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
    let session = NSURLSession.defaultSession()
    let task = session.dataTaskWithRequest(request) { (data, response, error) in
        //magic goes here
    }
    task.resume()
}

You can read more about NSURL session in this tutorial from raywenderlich.com

Hope that helps

sketchyTech
  • 5,746
  • 1
  • 33
  • 56
pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • Thank you very much. If I use this code, I get the error: "Type 'NSURLSession' has no member 'defaultSession'" which I don't understand. And what to fill in //magic goes here? – plategt Apr 13 '16 at 14:12
  • Sorry...it shouldn't be `NSURLSession.defaultSession()` but `NSURLSession.sharedSession()`. – pbodsk Apr 13 '16 at 14:22
  • As for the `//magic goes here part`, this is you callback which gets executed once the request has returned. You get some data, a response and an error as input parameters and then you typically check to see if any errors has occurred, if not, then you unwrap the data and map it to some model object which you can use in your viewcontroller or where you plan to use it. – pbodsk Apr 13 '16 at 14:24