0

I am trying to sign up an anonymous user using REST API. I found several discussion regarding linking Twitter/Facebook users that share the same problem but none gave this solution.

According to Parse.com REST API Doc, you only need to provide some sort of id to the authData, i.e UDID, but it complains that there is no password. This is the error:

"{"code":201,"error":"missing user password"}

This is my code:

func registerAnonymisUserWithComplitionHandler(deviceUDID:String, onComplition: RESTResponse){
let request = NSMutableURLRequest()
request.HTTPMethod = "POST"
request.addValue("APP ID", forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue("API Key", forHTTPHeaderField: "X-Parse-REST-API-Key")
request.addValue("1", forHTTPHeaderField: "X-Parse-Revocable-Session")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let urlString = "https://api.parse.com"
let baseURL = NSURL(string: urlString)
let url = NSURL(string: "/1/users", relativeToURL: baseURL!)

request.URL = url

let param = ["authData": ["anonymous":["id":deviceUDID]]]

let session = NSURLSession.sharedSession()
do {
    request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(param, options: .PrettyPrinted)
} catch {
    print("error")
}

let task = session.dataTaskWithRequest(request) {
    (data, response, error) in
    if (error == nil){
        do {
            let tmpUserDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
            print(tmpUserDictionary)


            let statusResponse = response as! NSHTTPURLResponse
            onComplition(jsonDictionary: tmpUserDictionary, response: statusResponse, error: nil)
        } catch {

        }
    } else {
        onComplition(jsonDictionary: nil, response: nil, error: error)
    }
}
task.resume()
}

Thanks to Muhammad Junaid Butt, I have fixed a small issue in the param and it works.

2
  • Why don't you use Parse iOS SDK? parse.com/docs/ios/guide Commented Jan 18, 2016 at 17:47
  • @MuhammadJunaidButt thanks for your comment. This would be the easy solution but my app needs to be build purely on REST API. The reason I need the above, is because I have another problem which is listed as a question, and I think this can solve it: stackoverflow.com/questions/34845398/… Commented Jan 18, 2016 at 17:50

1 Answer 1

2

Check the attached screenshot. I used REST API of PARSE to create an Anonymous user and it is working fine. (I used Postman for testing this).

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@TalZion Thanks for accepting my answer. I would suggest you to use AFNetworking or Alamofire to handle web-services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.