① many iOS apps support preferences such as saving usernames, passwords, font size, and more, iOS offers a standard set of solutions to add preferences to your app
② Each app has a Nsuserdefaults instance that accesses preferences
③ For example, save user name, font size, automatic login
Save method
-(Ibaction) Save {//1With Nsuserdefaults, you have direct access to the software's preferences (library/preferences) nsuserdefaults *defaults = [nsuserdefaults Standarduserdefaults]; //2. storing data [defaultsSetobject:@"MJ" forkey:@"Account"]; [DefaultsSetobject:@"123" forkey:@"PWD"]; [DefaultsSetInteger:Ten forkey:@"Age"]; [DefaultsSetBool:yes forkey:@"Auto_login"]; //3. sync now [defaults synchronize];}
Note: When setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to the timestamp. So after calling the set method, it is possible that the data has not yet been written to the disk application to terminate. The above problem can be forced by calling the Synchornize method to write
Read
- (IBAction)read { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *account = [defaults objectForKey:@"account"]; BOOL autoLogin = [defaults boolForKey:@"auto_login"];}
iOS Dev-store use preferences preference storage