Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save (Private) Application Settings on iOS?

I'm aware of NSUserDefaults for saving/restoring user preferences. What is the equivalent class for an application? For example, the application may have a "last run" field; or it may have a field for a unique identification of the device for use at the application level.

My intention is to keep the application's settings (not user's settings) out of the Settings Application, and not backup those settings in iTunes, Time Machine, {whatever}.

I'm getting a lot of noise for Java and C#, but not much for iOS/iPhone/iPad.

like image 387
jww Avatar asked Jul 08 '11 11:07

jww


People also ask

Can you have a secret app folder on iPhone?

You can hide apps on your iPhone so they don't appear on your home screen or in search results. To find an app that you've hidden, swipe to the last page of your iPhone's home screen and open the App Library. Hiding an iPhone app won't delete any of its data, and you can unhide it at any time.

How do I change the Privacy settings on my iPhone apps?

Go to Settings > Privacy, then tap App Privacy Report (iOS 15.2 or later). The App Privacy Report shows you how apps are using the permissions you granted them and shows you their network activity.


2 Answers

NSUserDefaults can be used for what you're asking.

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"shownPrompt"]) {     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"shownPrompt"];     // Show your prompt or whatever } 

That's a working code snippet. If the key is false, it sets it to true and shows the prompt. The next time this code runs, the key will already by true, so the prompt won't be shown.

NSUserDefaults is specific to the current app on the current device, and is similar to an NSMutableDictionary in that it's a key-value system, with the difference that instead of instantiating your own, there's a universal shared instance for your whole app, that doesn't get erased when the app exits.

NSUserDefaults is perfect for saving things like whether something has been shown, the date of last run, etc. Read the docs here: https://developer.apple.com/documentation/foundation/userdefaults

Don't be put off by the 'user preferences' part. You can use it to save anything you want (as long as it is or can be converted to an NSObject which implements <NSCoding>, which basically means NSString, NSDictionary, NSArray, NSNumber, UITextField, int, float, bool, etc.).

Just to clarify, stuff you put in NSUserDefaults will not, under any circumstances, automagically turn up in the Settings app. It will be kept completely private and hidden. For something to appear in Settings, you need to add a Settings bundle to your app, and manually add keys to it for each and every value that you want to be visible in the Settings app.

like image 198
Greg Avatar answered Sep 19 '22 00:09

Greg


if you can store value by NSUserDefaults, then it is good to store application preferences too.

or add settings.plist on your project and read that (what you are not changing later)

and you can use like.,

+ (NSDictionary*)getBundlePlist:(NSString *)plistName {     NSString *errorDesc = nil;     NSPropertyListFormat format;     NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];     NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];     NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                                           propertyListFromData:plistXML                                           mutabilityOption:NSPropertyListMutableContainersAndLeaves                                                      format:&format errorDescription:&errorDesc];     return temp; }  + (id) getPropValue:(NSString *)PropertyName {   // I am supposing you had add your app preferences on settings.plist.     return [[Property getBundlePlist:@"settings"] objectForKey:PropertyName];     //here Property is my class name, then you can use value by      //NSString *value = [Property getPropValue:@"setting1"]; } 
like image 30
Mujah Maskey Avatar answered Sep 17 '22 00:09

Mujah Maskey