User login status, iOS development user login

Source: Internet
Author: User

iOS development user logged in status, iOS developer login

The previous blog mentions the use of coredata for data persistence, coredata configuration and use steps are quite complicated. But familiar with the use of coredata process, CoreData still good use. What we are going to say today is how to log the status of our users. For example, QQ, etc., after the user login, close the application in the open directly landed. So how do we record the user's login status in app development? Before in PHP or Java to write the B/s structure of things, we use the session to store the user's login information, the session is present on the server only in a single reply to the effective, if you want to record the user's login status, then will use a cookie called something. Unlike a session, a cookie is a local file where the user's login information is stored and the user's login information is automatically read from the cookie when the user logs in.

Do not pull the Web, in our app development also encountered this problem, how to record the user's login status? With our CoreData, pretty? CoreData suitable for processing a large number of data, general and TableView to combine to use, of course, we can use CoreData to store the user's login information, but this is a bit overqualified, and it is more complicated to use. So do we have other, more convenient ways? Apple is so focused on user experience, of course there are other ways, that is nsuserdefault. Nsuserdefault to synchronize after setting the value

Nsuserdefault is a class that Apple provides to us to store the user's login information, of course, it is persistent, the nonsense is not much to say. We will simulate a user login and logoff demo to learn how to use Nsuserdefault to store the user's login information.

1. First we have to have a landing page corresponding to the class ViewController.h, on the landing page has two TextField and a button to accept the user's input, in the click on the callback method in the button, if the user login successful, Then the user information into the Userdefault, and then to the modal mode to jump to the main interface, first we want to get userdefault, the landing page of the login button callback method code is as follows:

12345678910111213141516171819202122232425 - (IBAction)tapLogin:(id)sender {        //获取用户输入的信息    NSString *username = self.usernameTextField.text;    NSString *password = self.passwordTextField.text;         //对用户信息的验证    if([username isEqualToString:password]){        //获取userDefault单例        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];                //登陆成功后把用户名和密码存储到UserDefault        [userDefaults setObject:username forKey:@"name"];        [userDefaults setObject:password forKey:@"password"];        [Userdefaults Synchronize];        //用模态跳转到主界面        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]];                id mainViewController = [storyboard instantiateViewControllerWithIdentifier:@"MainView"];                [self presentViewController:mainViewController animated:YES completion:^{        }];    }    }

?

2. After processing the landing page, we will handle the selection of which page to load based on whether the name attribute in Userdefault has a value. In the Storybaord we default is the main interface, in APPDELEGATE.M if not logged in, we will set up a Rootviewcontroller, the code is as follows

123456789101112131415 //判断是否登陆,由登陆状态判断启动页面//获取UserDefaultNSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];NSString *name = [userDefault objectForKey:@"name"];//获取storyboardUIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]];//如果用户未登陆则把根视图控制器改变成登陆视图控制器if(name == nil){    NSLog(@"%@",name);    id view = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];    self.window.rootViewController = view;}

?

3. After the above code we can be based on the user login status to select the loaded information, in the landing page, we add a button to exit. In the Exit button callback method, we need to remove the previously saved user information, and in a modal way to jump to the landing page, the code is as follows:

123456789101112131415161718 - (IBAction)tapLoginOut:(id)sender {        //获取UserDefaults单例    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];        //移除UserDefaults中存储的用户信息    [userDefaults removeObjectForKey:@"name"];    [userDefaults removeObjectForKey:@"password"];    [Userdefaults Synchronize];    //获取storyboard    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]];        //获取注销后要跳转的页面    id view = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];        //模态展示出登陆页面    [self presentViewController:view animated:YES completion:^{    }];

?

The following is in the storyboard, run out of the same effect as below, so we can record the user's login status

  

How to log program exit time in iOS development

What do you mean by quitting the process or by pressing the home key to return to the background?
If it's the former, there's really nothing to record.
If this is the latter, there is an agent method in the Appdelegate applicationdidenterbackground, when you press home, the method triggers

iOS development, how to save user name password

Upload Server Save: Local plist Save: Userdefault Save: SQLite Save

User login status, iOS development user login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.