I have 2 pages that I want to show as instructions the first time the app is run. How would I accomplish this?
You can utilize NSUserDefaults for something like this.
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"didLoad"]) {
//Show your instructions
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"didLoad"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
What this does is, check if the didLoad bool is FALSE (Which will be on first Load), if it is you show your instructions and set the BOOL to YES for the next Launch. You can put this in your viewDidLoad method.
Related
In my app used
[[NSUserDefaults standardUserDefaults] setObject:#[#"fr"] forKey:#"AppleLanguages"];
this func to change app language.
It working fine all labels,button and all other elements. but, it is not replicated in UIImagePickerController.
Language change when i kill and reopen the app.
pls any suggestion.
Advance thanks
You need to synchronize your NSUserDefaults and change it like this:
[[NSUserDefaults standardUserDefaults] setValue:#"fr" forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
Next you need to use the macro :
NSLocalizedString(string, nil)
for the text inside your UIImagePickerController.
I change in Info.plist file string Localization native development region to my language, and then in app UIImagePickerController changed language. Maybe and for you it will work.
Is it possible to store settings on the iPhone immediately? The apps on iOS 4+ don't close on exit, and if I press 'Stop process' in Xcode settings are not saved? How to save them?
- (void)compLoad {
compTotal = [[NSUserDefaults standardUserDefaults] integerForKey: #"compTotal"];
compCount = [[NSUserDefaults standardUserDefaults] integerForKey: #"compCount"];
compShow = [[NSUserDefaults standardUserDefaults] boolForKey: #"compShow"];
}
- (void)compSave {
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: #"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: #"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: #"compShow"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
synchronize - this method is automatically invoked at periodic
intervals, use this method only if you cannot wait for the automatic
synchronization (for example, if your application is about to exit) or
if you want to update the user defaults to what is on disk even though
you have not made any changes.
According to the documentation, the following call will persist any outstanding modifications. To check if this has been successful, check the return value of this call. A value of YES indicates success.
[[NSUserDefaults standardUserDefaults] synchronize];
Call your compSave in your AppDelegate
- (void)applicationWillResignActive:(UIApplication *)application
{
// Call your compSave here to force saving before app is going to the background
}
also
- (void)compSave
{
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: #"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: #"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: #"compShow"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Simply call [[NSUserDefaults standardUserDefaults] synchronize];. But usually you do not need to that: iOS will save once your app goes into background and also on a few other occasions. Calling synchronize too often is a considered to be a Bad Thing(tm).
Try using the synchronize method.
Update: you should consider registering your defaults, like it suggests in the Preferences and Settings Programming Guide:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Register the preference defaults early.
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:#"CacheDataAgressively"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// Other initialization...
}
Also, from this guide ("Synchronizing and Detecting Preference Changes"), regarding syncing:
To detect when changes to a preference value occur, apps can also register for the notification NSUserDefaultsDidChangeNotification. The shared NSUserDefaults object sends this notification to your app whenever it detects a change to a preference located in one of the persistent domains. You can use this notification to respond to changes that might impact your user interface. For example, you could use it to detect changes to the user’s preferred language and update your app content appropriately.
Pretty sure you still need to use synchronize if you want to re-read settings directly.
I have an application developed using iOS5. I have used the storyboard to design the whole screens. The problem I am facing right now is that, when the user runs the application for the very first time, I have to show a view for the user to register. I have no clue on earth on how to do it. I have created the registration forms on the storyboard itself.
Can someone put some light on this issue please?
Thankx in advance.
Try this (and also check this):
Set a value after the user starts the application:
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:1] forKey:#"registered"];
[[NSUserDefaults standardUserDefaults] synchronize];
and then check with this:
NSNumber *numb=[[NSUserDefaults standardUserDefaults] objectForKey:#"registered"];
if ([numb isEqualToNumber:[NSNumber numberWithInt:1]]) {
NSLog(#"registered");
}else{
NSLog(#"not registered");
}
I've created a modal view that loads on top of my tabBar to serve as a 3 step welcome screen.
While all of that works OK, the problem I'm having is finding a way to load it only once, so that the user doesn't have to deal with a welcome message on every single load.
I've done some research, and it looks like I might be able to call a method with NSTimer, but I'm not sure if that's a proper way to do it.
In your app delegate, set Bool and save in NSUserDefault Check if BOOL is set. If not then present view modally also set the BOOL.
Code might look like this:
In your appdelegate implementatuin. Application didFinishLaunching method:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"FirstTimeBool"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstTimeBool"];
// present view controller modally after this
}
You can store this information in NSUserDefaults that exactly what's it is for :
At first start :
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:#"IS_FIRST_LAUNCH"];
Then to know if it is the first time you run the app :
BOOL isFirstLaunch = [[[NSUserDefaults standardUserDefaults] valueForKey:#"IS_FIRST_LAUNCH"] boolValue]
Hope this helps,
Vincent
In reference to the following question: iPhone: How do I detect when an app is launched for the first time?
When I looked up NSUserDefaults apple reference, it said that registerDefaults does not store the data onto the disk. In the above question, the app registers the value firstLaunch to YES upon each launch. So each time the app is launched, firstLaunch is overwritten to YES and so the app will always think that this is the app's initial launch.
Am I right on this?
EDIT:
After doing what the tutorial above says, it doesn't always work anyway. I keep relaunching from Xcode and it keep printing out 1 bool value as in its the first launch.
registerDefaults: doesn't overwrite what is already in the defaults, but it sets defaults-for-the-defaults. So if a default is missing it uses a default-default. Confusing, isn't it?
If you've never written a value under the key FirstLaunch, then boolForKey:#"FirstLaunch" will use the value from registerDefaults:. However, if you did previously do setBool:NO forKey:#"FirstLaunch" and later ask for it again with boolForKey: then the value from registerDefaults: is not used.
registerDefaults: doesn't overwrite existing values, it only initializes values that aren't set to any value yet.
Try something like this in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([[[NSUserDefaults standardUserDefaults] valueForKey:#"firstRun"] intValue]==0) {
//do the stuff required at first launch
//after stuff done
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:1] forKey:#"firstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
NO first it will be nil then you set it to yes and before setting it to yes check if it's already set to yes or not. So first time userDefaults will return nil if you get something for a key. like
[[NSUserDefaults standardUserDefaults] valueForKey:#"FirstLaunch"];
will return nil on first launch of application.