How to integrate with Pagseguro using Uiwebview on Swift

Asked

Viewed 325 times

1

I’m trying to integrate Pagseguro with my Swift application using Uiwebview, using POST method, but it didn’t work. following error being returned and the part of my code where I make the call:

error Nserror Domain: "Nscocoaerrordomain" - code: 3840 0x79741210

    let URL = NSURL(string: "https://ws.pagseguro.uol.com.br/v2/checkout/")

    let mutableURLRequest = NSMutableURLRequest(URL: URL!)

    mutableURLRequest.HTTPMethod = "POST"

    let parameters = ["email":"[email protected] ",
        "token":"PUB9DC3C03DD97D4FB4AB1EE70161FE3063",
        "itemId1":"0001",
        "itemDescription1":"Notebook Prata",
        "itemAmount1":"24300.00",
        "itemQuantity1":"1",
        "itemWeight1":"1000"]

    do {

        mutableURLRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions())

        mutableURLRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        mutableURLRequest.addValue("charset=ISO-8859-1", forHTTPHeaderField: "Content-Type")

        let config = NSURLSessionConfiguration.defaultSessionConfiguration()

        let session = NSURLSession(configuration: config)

        let task = session.dataTaskWithRequest(mutableURLRequest, completionHandler: {

            (data, response, error) in

            guard let responseData = data else {

                print("Error: did not receive data")

                return

            }

            guard error == nil else {

                print("error calling POST on /posts/1")

                print(error)

                return

            }

            let post: NSDictionary

            do {

                post = try NSJSONSerialization.JSONObjectWithData(responseData, options: []) as! NSDictionary

            }

            catch {

                print("error parsing response from POST on /posts")

                return

            }

            print("resultado: ")

            print(post)

        })

        task.resume()

    }

    catch {

        print("Error parsing response from POST on /posts")

        return

    }
  • you have already checked if the JSON you are receiving is valid? This usually happens when Response is a JSONP.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.