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