0

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.

3 Answers3

3

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)")
}
Yogendra Girase
  • 631
  • 3
  • 15
0

When you get a success response from login API. write this line after your response and before you push to homepage.

[[NSUserDefaults standardUserDefaults] setObject:@"Token" forKey:@"accessToken"];

I hope it will work for you.

vegda neel
  • 154
  • 12
0

It is very simple to achieve using NSUserDefaults

Step 1: When login API called and got the response, then save the accessToken in user defaults, like:

[[NSUserDefaults standardUserDefaults] setObject:@"yourAccessToken" forKey:@"AccessTokenKey"];

Step 2: Retrieve the saved accessToken you have saved in user defaults whenever you required throughout the project, like:

NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"AccessTokenKey"];

Happy coding...

Sailendra
  • 1,318
  • 14
  • 29