I can not make devicetoken post

0

I have configured the notifications using GCM and to send them later, I need to register the tokens in my database. First of all in my AppDelegate, I store the token in the following way:

let deviceTokenString = NSString(format: "%@", deviceToken) as String

NSUserDefaults.standardUserDefaults().setObject(deviceTokenString, forKey: "deviceToken")
NSUserDefaults.standardUserDefaults().synchronize()

After this, in another screen I make a Post that I have verified that it works since a certain value or even another variable is published without any problem, but the token does not do it. For example, in this code, the variable otra would be published but tokenstring does not.

var tokenstring = NSUserDefaults.standardUserDefaults().objectForKey("deviceToken")
var otra = "prueba"
...

let request = NSMutableURLRequest(URL: NSURL(string: "http://elpenitente.playcofrade.com/post/registrar-ios.php")!)
        request.HTTPMethod = "POST"
        let token = "token=\(self.tokenstring)&so=ios"

        request.HTTPBody = token.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {
                print("error=\(error)")
                return
            }

            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()

On the other hand, I created a function to be able to see the value stored in that value of NSUserDefaults and the token is shown so the failure should not be there either. What can be happening?

    
asked by Javi Rando 02.03.2016 в 15:53
source

2 answers

1

After viewing your file, I recommend that you directly use Alamofire such that:

func showAlert(title:String, message:String) {


    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    let acceptAction = UIAlertAction(title: "Aceptar", style: .Default, handler:{  action in

        Alamofire.request(
            .POST,
            "http://elpenitente.playcofrade.com/post/registrar-ios.php",
            parameters: ["token": \(self.tokenstring), "so": "ios"]
            ).responseJSON(completionHandler: { (response) -> Void in

                // Tratar las respuesta

            })

        self.performSegueWithIdentifier("inicial", sender: self)

        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "TermsAccepted")

    })

    alert.view.tintColor = UIColor(red:0.33, green:0.0, blue:0.34, alpha:1.0)

    alert.addAction(acceptAction)


    self.presentViewController(alert, animated: true, completion: nil);

    self.prueba.text = message


}
    
answered by 03.03.2016 / 20:21
source
1

Passing the body as if they were parameter data of URL does not give me a good spine. Try the following variant:

let request = NSMutableURLRequest(URL: NSURL(string: "http://elpenitente.playcofrade.com/post/registrar-ios.php?token=\(self.tokenstring)&so=ios")!)

Let's see if this way it reaches the server or that it is ...

On the other hand, if you pass the data by the body , what you have to do is a POST and you should change the format with what you spend, for example to JSON You can try to do the request such that:

let request = NSMutableURLRequest(URL: NSURL(string: "http://elpenitente.playcofrade.com/post/registrar-ios.php")!)
request.encodeParameters(["token": self.tokenstring, "so": "ios"])
request.HTTPMethod = "POST"

And then launch the POST .

In any case, it is difficult to see what is happening without being able to see what you send, receive, etc.

    
answered by 02.03.2016 в 16:08