X Bourasseau
X Bourasseau

Reputation: 31

Getting PHP output with swift

I'm very new on stackoverflow & very very new with Swift. I need to get result from PHP, using Swift code like the following :

func GetEtatO2()
{
    let url = NSURL(string: "http://www.exxagon.com/php/GetEtatO2.php")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!,
        completionHandler:
        {
            (data, response, error) -> Void in
            if error != nil
            {
                print("Error: \(error)")
                return
            }
            else
            {
                print("data ==> \(data)")
                print("response ==> \(response)")
                print("error ==> \(error)")
                let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
                print("responseString ==> \(responseString!)")
                self.EtatO2 = responseString!
            }
        }
    )
    task.resume()
}

The string returned by my PHP code is "ON" ou "OFF".

I can get this string the else part of my code, but, the line self.EtatO2 = responseString! seems to do nothing; I mean the value "ON" ou "OFF" is not stored in var EtatO2.

I'm sure I miss something bigger than me ... can you give some help ? Many thanks is advance.

Upvotes: 2

Views: 121

Answers (2)

X Bourasseau
X Bourasseau

Reputation: 31

I finally found a solution : inserting

 dispatch_async(dispatch_get_main_queue(), { () -> Void in  ...
as follow
`@IBAction func GetEtatO2(sender: UIButton) {
    let Url = NSURL(string: "http://www.exxagon.com/php/GetEtatO2.php")
    let task = NSURLSession.sharedSession().dataTaskWithURL(Url!, completionHandler: { (data, response, error) -> Void in
            print( ">>>> Debut >>>>")
            print("Error: \(error)")
            if error != nil
            {
                return
            }
            else
            {
                print("data: \(data)")
                print("Response: \(response)")
                let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
                print("responseString: \(responseString)")
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.EtatO2Label.text = responseString
                })
            }
            print( "<<<< Fin <<<<")
    })
    task.resume()
  }

By this way, pressing GetEtatO2 button, display the right status in a Label.

Upvotes: 1

Sidetalker
Sidetalker

Reputation: 708

Make sure you're not trying to use self.Etat02 until after the completion handler is called. One way to do this is to add a completion handler to GetEtat02().

func GetEtatO2(completion:(String) -> ())
{
    let url = NSURL(string: "http://www.exxagon.com/php/GetEtatO2.php")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!,
        completionHandler:
        {
            (data, response, error) -> Void in
            if error != nil
            {
                completion("Error: \(error)")
            }
            else
            {
                let responseString = String(data: data!, encoding: NSUTF8StringEncoding)

                completion(responseString!)
            }
        }
    )
    task.resume()
}

Then you are able to call GetEtat02() and use the response like this:

GetEtatO2() { responseString in
    // This won't run until the NSURLSession completes
    print(responseString)
}

// responseString doesn't exist here, only in the closure above

Upvotes: 0

Related Questions