Nivesh Singh Nivesh Singh - 16 days ago 5
iOS Question

how can i store my access token to defaults or into an array

i want to hit an API at login then i'll get a access token then whenever i"ll run my app it moves directly to homepage but m unable to get the access token there,how can i achieve this.my homepage Conditions are not getting access token simply.

Answer

You can use user defaults objective c save your token

NSString *token = @“yourToken”;
[[NSUserDefaults standardUserDefaults] setObject: token forKey:@“Token”];
[[NSUserDefaults standardUserDefaults] synchronize]; 

Get your token

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"Token"];

Swift3.0 code save data

let defaults = UserDefaults.standard
defaults.set(“yourToken”, forKey: “Token”)

// Get the Token from UserDefaults
if let token = defaults.value(forKey: “Token”) as? String {
    print("defaults Token: \(token)")
}