开发者

How to use NSUserDefaults for creating the auto login settings for application

I know this very regular question But i didn't get that's way ask here I hope people will me out . I have application which have the login page .And when ever I login I get Token_ID from server .I get this TokenID with help of JSON.

But I want to add the Auto Login function to my application .For that My application look like this

  1. It has the two text field (username and password)
  2. one check box for auto login And login button

I need when ever user click on the check box which is for auto login It must store TokenID in application and when I close my application .And start the application a开发者_运维知识库gain It must not ask for login me again.It start the same application with same TokenID which I store for that user not a new one .And if I click the log out button it must logout from application And release the TokenID from my application .And after that if start the application it must ask for login with out that it must not go further in application

For that I do R&D from two days .And I found that I must use the NSUserDefaults for storing the TokenID which I am getting at the time of login .I want know how can I store the TokenID into application and How can check it out the user logout or not .And if user not logout it start the application.And if user logout than it must ask for login to start the application

OR is that any idea please give And explain me Thank you


First result at google for "NSUSerDefaults Example"

On logout just overwrite the keys with nil and check on starting of the app if the key is nil


you can save username information

-(void)saveToUserDefaults:(NSString*)myString
 {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:myString forKey:@"username"];
        [standardUserDefaults synchronize];
    }
 }

if you have username information on NSUserDefaults, you can get your username information

 -(NSString*)retrieveFromUserDefaults
 {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *val = nil;

    if (standardUserDefaults) 
        val = [standardUserDefaults objectForKey:@"username"];

    return val;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜