Keeping variable values when the iPhone is shut-down? - iphone

Lets say i have an application which has three text-fields, and i can type whatever i want into them, lets also assume that i have a checkbox and a button. And if the button is tapped while the checkbox is checked, the nsstring values in these textfields should get saved somehow. Lets say i power down my iPhone and restarted it, opened the app once again and wanted those values to be in their respective textfields.
How does one do this?
Is this a case for NSUserDefaults or something for Apple's own Keychain API to handle?
Cheers.
Edit: We used local declarations when setting and getting the values of the NSUserDefaults, which of course, doesnt work. It works perfectly now...

Yes you can make use of NSUserDefaults

Integrate a sqlite3 database into ur app.. save the textfields value in an array and sav it in the database. On starting of the apps just load from the database and retrieve the top most array and show the values back as output.. Hope it helps.. I am new also.. but cant think of anything else if u want to restart ur whole phone and still wants the fields to be filled.

Use NSUserDefaults for stuff that doesn't need to be stored securely and Keychain if it does.

You can use the NSUserDefaults or a PList or a database to save the content.
Example is given below:-
[[NSUserDefaults standardUserDefaults] setObject:#"Jayahari" forKey:#"userName"];
PList can be used for archiving, serilizing. Database also can be used for same purpose.

Related

How to save ALAssetsLibrary to usersDefault when i close my app?

When i save images in ALAssetsLibrary, it's ok to show them in my table, but when i close my app from multitasking bar - records is cleared. How to save them and stay load in my table when i run my app again???
I found this Saving ALAsset URL in NSUserDefaults
I think you want to save resources so that you can use them later. If it does, you can save the path of resource to your user defaults or core data, and store them into "Photo Album". When you need it, just use the path in string as the resource path to get it.
Take a look at ALAssetsLibrary+CustomPhotoAlbum, maybe you'll like it. ;)

Change iphone app language with buttons and without restart of the app

I want to change the language of my app. At the moment I am doing it at the following way.
I have two buttons which are change the languages in NSUserDefaults. But before this affects my app I need to restart it.
Here is my code.
- (IBAction)changeDutch:(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"nl", #"en", nil] forKey:#"AppleLanguages"];
}
- (IBAction)changeEnglish:(id)sender {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", #"nl", nil] forKey:#"AppleLanguages"];
}
This works fine. But I don't want every time to restart my app. Can anyone tell me how to do that?
Kind regards!
Check out HMLocalization: https://github.com/HeshamMegid/HMLocalization
It's a replacement for the default localisation framework. It has a demo showing how to change language without having to restart the app.
There is a method to change app language without restart mentioned this tutorial post I've tried it in an app and it works mostly but still if you are using system items like More tab in tabbar, Edit button on MoreNavigationController and Cancel button on UISearchBar and so on, there text can't be changed for selected language without restarting app. If there is no such item that is controlled by iOS instead of your app, this is a perfect solution for you.
If your data is in UITableView then you could use [tableView reloadData];.
You can even set the app languages in appDelegate, defined in Constants, keys, then on the IBAction call those keys then be stored in NSUserDefaults.
which way you're using for localization, I mean to ask is that native in which we just only maintain the folder structure or taken from Db.
Anyways, If you're using native part, then look at following link which may assist you as I think we can replace the language option there:
"http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios"
(Frankly speaking, I've no idea)
But if you are using the db part, then in that case you just use some enum based tact and according to which just fetch your data, that approach is simple as I've already applied in so many apps.
In any concern, just back to me. :)

iPhone application preferences

I've been doing some research and so far I've been unable to find out how to display a Settings.bundle inside an application. The guides that I found are:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/Preferences/Preferences.html
http://blog.webscale.co.in/?p=274
http://knol.google.com/k/iphone-sdk-application-preferences#
These all seem to skip a step: How to display the plist from a view in your application. I've got my view set up and my plist file set up, but I have no idea how to display the preferences plist from the view. Do I manually load everything and put them into labels, switches and whatnot?
This may seem like a stupid question but I honestly don't know. Would anyone be able to explain this to me?
Thanks in advance
EDIT: To clarify; I've found out that this allows me to access the preferences through the settings app. However I want to access these through a tab in the app itself.
http://knol.google.com/k/iphone-sdk-application-preferences#Step_4(3A)_Retrieving_Values_of_Settings appears to describe exactly how to access these...
It's a one-liner so i'll paste it here:
NSString* settingValue = [[NSUserDefaults standardUserDefaults] stringForKey:#"<Setting Key>"]
There's no magical setting key that you have to use. Use whatever you like, and it'll be there the next time the application loads.
I got the same problem; I have an application with a tabBarController as the RootController of my application and, separately, I have a settings menu (loaded from plist) available through the applications preferences of the device.
I do want to include this settings menu into my app as an other view for my tabBarController in order to avoid to have to exit my application to change the settings.
Finally, I have the plist file ready, I just want to know a way to do something like
[C#]
settingsController.View = UIView.LoadFromPList("settings.plist");
tabBarController.addController(settingsController);
... anybody knows ?

Where to initialize iPhone application defaults

I just started using the preferences pane to let users customize some settings in my iOS app, and it was pretty easy to create the Settings.bundle and access the information from it. My problem is that I read in the Apple docs that the settings should be programatically initialized:
It is recommended that you register any default preference values programmatically at launch time in addition to including them in your settings bundle property lists. For newly installed applications, default preference values from the application’s settings bundle are not set until the Settings application runs. This means that if the user runs your application before running Settings, the default values specified in your settings bundle will not be available. Setting such values programmatically at launch time ensures that your application always has appropriate values. To register default values programmatically, use the registerDefaults: method of the NSUserDefaults class.
Where in the app is this initialization done, and how can I be sure that I'm not overwriting a user-supplied value? Is this handled in some method of the App Delegate?
You should register your defaults before you try to access a value stored in your NSUserDefaults.
You could do it in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
Registering your defaults is fast, so there is no need to optimize this. Just do it at the launch of the app.
I store my userdefaults in a plist and register the content of this list, like this:
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"DefaultUserDefaults" ofType:#"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
If you register your defaults like this you don't have to worry that you overwrite user supplied values.
NSUserdefaults uses "domains" where it stores it's values. If you register your defaults they are stored in the registration domain. If a User stores a value those values are stored in the application domain.
If you try to get a value from NSUserdefaults it looks if the value is present in the application domain, and if it's not there it takes the value from the registration domain.
Edit:
you would access those values (or better, the values that are stored in your nsuserdefaults, and those as a fallback if there are no user provided values) like this:
NSInteger minutesToWait = [[NSUserDefaults standardUserDefaults] integerForKey:#"MinutesToWait";
NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:#"DefaultURLStr"];
The plist is just another representation of a NSDictionary with keys and values. The key is the same key you use to access the userdefaults, and the value is your defaultvalue.
Pretty straight forward.
It doesn't matter how you create the dictionary. You can do it in code as well.
As #fluchtpunkt suggested, you can register the defaults using:
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"DefaultUserDefaults" ofType:#"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
Personally, I check for each value independently in my App Delegate.
#define kSettings [NSUserDefaults]
if([kSettings boolForKey:#"firstRun"] == nil){
//
// Set up the initial settings...
//
[kSettings setBool:NO forKey:#"firstRun"];
}
I write a "reset" method and then call it on first run. I prefer doing this in code, but you theoretically could use a plist. I just feel like it's one less place to go wrong.
You can use the same method inside your app delegate that you use to setup your initial window, didFinishLaunchingWithOptions:
However, you may also need some logic inside applicationWillEnterForeground:, because potentially your user could put your app into the background, change settings inside the settings app, then resume your app and expect those changes to have been applied.
#MatthiasBauch or #Moshe's answers will most likely work for most people, but for anyone who like me had their settings in a Root.plist file (I was using the InAppSettingsKit), you have to dig a bit deeper into that particular plist file to get the actual default values of the settings (since they're not at the top level of the plist, but nested under the key PreferenceSpecifiers). Here is a link to another answer here, containing the extra code that worked for me:
https://stackoverflow.com/a/10497898/381233

How I do to call a nib file at only first launch of my application?

I'm new here but I'd like to learn very well iPhone SDK...
I'm making an iPhone app where I'd like to show a modalView controller at launch of my app... How can I do this?
In this modalView, I request some informations and the view must appear only when these informations aren't saved!
Anyone can help me?
P.S.: Sorry for my bad English but I'm Italian... :D Thanks!
First, try to avoid to use NSUserDefautls as it's not application specific and can cause troubles under some circumstances (see reference docs).
I'd suggest to save your app specific data to some plist file for which you can check for at app startup -(void) applicationDidFinishLoading: method of your app delegate class - and decide if your modal view should be shown or not.
Let's say you have application wide accessible
NSMutableDictionary instance where you store your
preferences. When app is about to quit i.e. - (void)
applicationWillTerminate: method of your app delegate, simply store content of that dictionary to plist somewhere under you app directory structure (Documents folder is a good choice). See NSDictionary reference on how to store/read plist files. It's pretty simple.
Typically, you check for your saved data and if it's not there, you assume that it is the first run.
Thus, you first need to decide how you are going to persist data:
User defaults (NSUserDefaults).
Store a file. Typically a property list (plist) in the Documents directory.
Core Data.
I'd like to use NSUserDefaults...