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.
            Asked
            
        
        
            Active
            
        
            Viewed 953 times
        
    0
            
            
        - 
                    For using NSUserDefaults. refer http://stackoverflow.com/a/3074489/5215474 – Saranjith Nov 22 '16 at 05:56
- 
                    Consider using the keychain instead: http://stackoverflow.com/a/16795674/433373 – Nicolas Miari Nov 22 '16 at 06:00
- 
                    Possible duplicate of [Save string to the NSUserDefaults?](http://stackoverflow.com/questions/3074483/save-string-to-the-nsuserdefaults) – User511 Nov 22 '16 at 06:00
3 Answers
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
- 
                    
- 
                    ok, I have updated my code, I have added this becoz I had coded this in xcode 5. So I put the code here. Thanks – Sailendra Nov 22 '16 at 06:53
