IOS development: application setting implementation

Source: Internet
Author: User

Many apps have related settings in IOS settings, such:



Through this setting, you can easily modify some basic settings of the application.

To fully implement this setting function, you need to solve the following problems:

1) Write settings (implement the setting function)

2) storage of the set content (ensure that the setting takes effect immediately after modification. For example, to minimize the application, change it in settings, and re-open the application, a new setting method should be displayed)


For more information, see the apple development documentation preferences and settings programming guide.

The implementation process is as follows:


Step 1: Write settings

Use settings bundle.

1) create a new settings Bundle:


After creation, you can see the following content:


As we can see, the settings bundle contains two files, one is root. plist used to set the content to be set, and the other is root. strings used for multilingual. We do not consider multi-lingual content. We only look at the setting method of root. plist.

We can see that the root. plist file already contains several items in advance, which are the content to be set. There are 7 types:


These are easy to understand, except that child pane is a number of controls, but it is implemented through the plist file here. Child pane is a child page, which may be relatively troublesome. Let's talk about the first few.


First, let's take a look at the expected results:



1) Group

The settings are as follows:


2) Text Field

The settings are as follows:


The identifier here is the key used for subsequent storage, that is, using nsuserdefaults, which will be introduced later.

Then the text field is secure option is to set whether to encrypt. Others are easy to understand.

3) Toggle switch


4) Slider


You can add left and right images.

5) multi value


The settings are as follows:



The following describes the use of child pane.

To add a new settings page, you must create a new plist.


From the development documentation, you can see the structure clearly.

Therefore, we need to create a new plist file.

The key point is that we cannot create in xcode, because in xcode, we cannot pull files into bundle. Therefore, the simplest way is to open the bundle File Manager and copy the root. plist, and rename it !!!



Next, set child Pane:


Filename is the name of the new plist without a suffix.

Then, the editing in child. plist is the same as that in root. plist. The key point is to set the type of child. plist to iPhone settings plist for ease of editing.



OK. Through the above method, we can complete the required settings on the interface. The following are data operations.


Step 2: Use nsuserdefaults to store preference.

First, it is clear that the above setting data will be stored in standarddefaults of nsuserdefaults, And the identifier corresponding to each setting is the key.


1) In general, we need to set some initial data, that is, the data can be set for the first time the application opens.

Code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.        NSDictionary *defaults = @{kScreenAutoLock: @NO,                               kSensitivityValue: @5};    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];    return YES;}

You can set a dictionary. Kscreenautolock is the key defined by macro, that is, identifier.

2) obtain data

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];        value = [defaults floatForKey:kSensitivityValue];    switch  = [defaults boolForKey:kScreenAutoLock];

3) Change Data

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    [defaults setFloat:5.0f forKey:kSensitivityValue];    [defaults synchronize];

Remember to use synchronize to store data.


4) Supplement

It is not enough to use the above method alone. The problem is that if we change the settings in the settings, this is because the data is not synchronized after the application is started, or the data before the configuration. Therefore, when the program enterforeground is used, the application should synchronize data.

In use, the changed data is synchronized during enterforeground through notification.

Code:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

-(Void) applicationwillenterforeground :( nsnotification *) Notification {[[nsuserdefaults standarduserdefaults] synchronize]; [self refreshsettings]; // update settings display}

This section describes settings.









Related Article

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.