Reputation: 1347
I am using a private api to register users.
Api structure is liked this:
post data = {
"email": "",
"password": ""
}
------------------------------------------
return value = {
"result": True,
"message": "",
"token": ""
}
My code is:
Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON)
.responseString { response in
guard response.result.error == nil else {
print(response.result.error!)
return
}
print(response.result.value)
if let value:String = response.result.value {
let post = JSON(value)
if let connection = post.dictionary {
let token = connection["token"]?.string
print(token)
}
}
}
reponse.result.value returns an HTML file and I can not get "result", "message" and "token" values. I tried several methods I find from github but couldn't get it work.
EDIT:
I managed to get response from the api: SUCCESS: {"message": "you are already registered", "result": false, "token": asdwasdasd}
How I am going to get for example "token" and save it?
Upvotes: 0
Views: 670
Reputation: 1347
I found a solution, now its working as I wanted...
Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON)
.responseJSON { response in
guard response.result.error == nil else {
print(response.result.error!)
return
}
if let json: NSDictionary = response.result.value as? NSDictionary {
let result = json["result"]!.stringValue
print(result)
let token = json["token"] as! String
print(token)
}
}
Upvotes: 0
Reputation: 6812
For saving tokens, I would recommend putting it into the keychain. If you are familiar with NSUserDefaults, the keychain acts similarly except that it adds a security layer to what you're storing. (for more information on how the keychain works, look at Apple's Keychain Documentation)
Using keychain through Apple's API can be a bit convoluted, so I use the KeychainAccess library to give me a friendlier API. The UICKeyChainStore documentation gives a very succinct tutorial on how to use the library so I will reference you there for implementation (see KeychainAccess link above).
Upvotes: 1