ios - How to use access token from response using swift 3 -


so making login application sends username , password, , server replies access token....am trying save token later use cant. here code, , server response incase need import uikit

class viewcontroller: uiviewcontroller {      @iboutlet weak var phonenumber: uitextfield!      @iboutlet weak var password: uitextfield!     //***************************************login**********************************************\\     @ibaction func login(_ sender: uibutton) {          guard let url = url(string: "https://goollyapp.azurewebsites.net/token") else {return}         var request = urlrequest(url: url)         request.httpmethod = "post"         let poststring = "username=" + phonenumber.text! + "&password="+password.text!+"&grant_type=password"         print(poststring)         request.httpbody = poststring.data(using: .utf8)         let task = urlsession.shared.datatask(with: request) { data, response, error in             if let response = response {                 print(response)             }         }         task.resume()     } } 

where declare json variable replace with:

let json = try? jsonserialization.jsonobject(with: data, options: []) as! [string: any] 

then can access token with:

let token = json["access_token"] as! string 

(after receiving successful json response)

you can implement singleton follows:

class token {      // can't init singleton     private init() { }      static let sharedinstance = token()      var tokenstring = ""  } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -