Is There A Way To Make "Invisible" User Defaults?
My last question was going this path before, but I think this warrants a new question because I want to to go for this path now (instead of attempting to do what I was trying to do in my other question).
What I want to do is to have "Invisible" values in NSUserDefaults. I don't care about Apple's guidelines because my app is aimed at Jailbroken phones. This is what I have so far:
The root.plist in settings.bundle looks like this (so far, because I want to add a password fie开发者_开发百科ld later):
first_launch is a value that I don't want the user to modify directly. As it's name may imply, it's value will be "false" once the user launches the app once and goes through my small configuration wizard.
Like you can see in the plist, first_launch has TRUE/YES as it's initial value, yet this code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%d", [defaults boolForKey:@"first_launch"]);
ALWAYS show me it's 0. What can I do to have "invisible" values in my settings bundle? I don't want the user to modify first_launch for obvious reasons, and I always want to add a password field that will only be accessible from the app itself.
NSUserDefaults
values are invisible to the Settings app unless they are added in a Settings.bundle
.
You can store information in NSUserDefaults by using this code:
[[NSUserDefaults standardUserDefaults] setObject:someObject forKey:@"stringKey"];
So, in your app delegate, try this:
if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] != NO){
// Do first run stuff here...
// Then, set the firstRun flag to NO
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
}
EDIT:
Switches and Grouped data and the like must be initialized in code or set by the user for them to work.
Edit2:
You need to manually change value of the @"firstLaunch"
key.
if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"] == NO){
NSLog(@"The app has never been launched before.");
// Notice the changed value here:
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
} else {
NSLog(@"The app was launched before.");
}
精彩评论