I have an app where Location is important. Currently I have a multi-value setting in the settings bundle where I have 5 locations defined. The problem with this approach is that the settings bundle is static - i.e. I cannot update that from a JSON list on my server as far as I know.
I want to update the location list from a dynamic list on the server.
I have looked at InAppSettingsKit but this also uses the standard settings bundles. Is it possible to use InAppSettingsKit to import settings updates dynamically from a remote list.
Are there other ways to do what I am trying to do?
You won't be able to change the list dynamically with regards to Settings.app. Settings.app always uses the static schema plist from your app bundle. (You could resort to a freeform text field but that probably doesn't catch your case.)
With InAppSettingsKit, you can accomplish that but you have to do some extra work: For the dynamic parts, you'll want to use a custom view controller, e.g. a table view controller.
Best way for you handle this is storing the multi-values in the NSUserDefaults and start using them. In this method, you will be able to update the values as well as it will be persistent across multiple sessions.
Edit (answers comment):
Saving it to user defaults.
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
#"your Object", "Your Key"
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];
Retriving it:
[[NSUserDefaults standardUserDefaults] integerForKey:#"Your Key"];
Related
I'm having trouble understanding how I can have dynamic data fed into a MultiValue attribute inside the Root.plist file of a Settings-Watch.bundle.
When fetching some REST API I would like to retrieve some map (dictionary) and set that data as the list of choice in my Watch Settings Bundle so that the user can select a specific value from it inside the Apple Watch application on the iPhone/iPad.
I tried like this:
NSUserDefaults *watchPrefs = [[NSUserDefaults alloc] initWithSuiteName:#"group.foo"];
// foo and titles are NSArray* of NSString.
// I also tried by giving it a NSDictionary* whose keys are what's in foo and values reflecting bar
[watchPrefs registerDefaults:#{#"preferedAccount": #{#"Values": foo, #"Titles": bar}}];
[watchPrefs synchronize];
But even when I close the app these data are not shown in the Watch application > My application.
I configured the preferedAccount as a MultiValue inside the Root.plist file.
It must be dynamic in that case because the data reflects the logged user and so cannot be known in advance.
Is it even possible to display dynamic data there ?
Thanks
I can't figure out how to make the create an account function work in my application... I have my login system set up like so: http://imgur.com/dKGRnok The first set of strings are the passwords. The second set are the usernames. For someone to create an account that allows them to login, I have to make a string manually through the application. How can I make this function so it gets their input and places it as a string as long as the same username doesn't already exist? http://imgur.com/lVzAV8i
You need to create a local database, the proper way would be to use either sqllite or core data to create your structure, then simply add/remove users.
For an sql tutorial for ios follow this:
http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app
Or go to this site and check the section that says "Saving and Loading Data"
http://www.raywenderlich.com/tutorials
I personally prefer core data because its the most complete solution once you get used to setting it up.
If what you are asking is a persistent data, [[NSUserDefaults standardUserDefaults] setValue: forKey:] will be the answer.
Once you save a data with NSUserDefaults, the data will be persistent. You can get the value with [[NSUserDefaults standardUserDefaults] valueForKey:]
Added
I misunderstood your question. In this case you should use local database as Chiques said.
Core Data will be the answer.
http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
Can I know how exactly NSUserDefaults works?
I'm using it to maintain user info like username.
In one controller I set :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"xyz" forKey:#"username"];
and in another one I retrieve it as :
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
username = [prefs stringForKey:#"username"];
It works sometimes, but sometimes the setobject doesn't set anything ( username = [prefs stringForKey:#"username"]; gives me nil. Sometimes it works fine. I thought this was a persistent storage so I'm not sure what's happening. This is in simulator as I haven't got the chance to test it on a phone yet.
This is what Mac Developer Library is saying about NSUserDefaults
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide.
Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for "MyStringDefault", the string you later retrieve using stringForKey: will be immutable.
A defaults database is created automatically for each user. The NSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API (see Preferences Utilities Reference). However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferences code with NSUserDefaults code.
If your application supports managed environments, you can use an NSUserDefaults object to determine which preferences are managed by an administrator for the benefit of the user. Managed environments correspond to computer labs or classrooms where an administrator or teacher may want to configure the systems in a particular way. In these situations, the teacher can establish a set of default preferences and force those preferences on users. If a preference is managed in this manner, applications should prevent users from editing that preference by disabling any appropriate controls.
Would u mind trying this.
[[NSUserDefaults standardUserDefaults] setObject:#"xyz" forKey:#"username"];
than doing like the way u are doing .
And Check the Value by using
NSLog(#"%#",[NSUserDefaults standardUserDefaults] objectForKey:#"username"]);
I've got a pretty simple task that I'm trying to accomplish in Objective-C.
I make a call to a web site, and get a value from the web site through an HTTP call. I want to save this value to disk so I can retrieve it later.
What would be the best method to do this - a text file, or in the PList file?
It's just one value that may occasionally be updated. The call to the web site is made on-demand.
Consider using NSUserDefaults for storing single values
For Storing:
[[NSUserDefaults standardUserDefaults] setInteger:100 forKey:#"storageKey"];
For Retrieving:
NSInteger myValue = [[NSUserDefaults standardUserDefaults] integerForKey:storageKey];
What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.