Does the defaultvalue of settings. Bundle not work? (Nsuserdefaults)

Source: Internet
Author: User

The first time I needed to use the iPhone system configuration, I found that the defaultvalue in settings. Bundle/Rool. plist does not work. The returned value is nil. After Google, nsuserdefaults does not provide a ready-made method for defaultvalue to take effect. The solution is as follows:

Check the set item values in the program's-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {}. If it is nil, it is all set to defaultvalue:

 

 // [Yourapp] appdelegate. m
-(Bool) Application :( uiapplication *) Application
Didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {

Nsstring * name = [[nsuserdefaults standarduserdefaults]
Stringforkey: @ "name_preference"];
If (! Name ){
// Load the default configuration
[Self defined mselector: @ selector (registerdefadefasfromsettingsbundle)];
}

[Window addsubview: viewcontroller. View];
[Window makekeyandvisible];

Return yes;
}
// Obtain the default settings
-(Void) registerdefaultsfromsettingsbundle {
Nsstring * settingsbundle = [[nsbundle mainbundle] pathforresource: @ "Settings" oftype: @ "bundle"];
If (! Settingsbundle ){
Nslog (@ "cocould not find settings. Bundle ");
Return;
}

Nsdictionary * settings = [nsdictionary dictionarywithcontentsoffile: [settingsbundle stringbyappendingpathcomponent: @ "root. plist"];
Nsarray * preferences = [settings objectforkey: @ "preferencespecifiers"];

Nsmutabledictionary * defaultstoregister = [[nsmutabledictionary alloc] initwithcapacity: [preferences count];
For (nsdictionary * prefspecification in preferences ){
Nsstring * Key = [prefspecification objectforkey: @ "key"];
If (key ){
[Defastorstoregister setobject: [prefspecification objectforkey: @ "defaultvalue"] forkey: Key];
}
}
[[Nsuserdefaults standarduserdefaults] registerdefaults: defaultstoregister];
[Defastorstoregister release];
}

 

 

Download defaultsettingsdemo

 

Ref: http://stackoverflow.com/questions/510216/can-you-make-the-settings-in-settings-bundle-default-even-if-you-dont-open-the-s

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.