In my iPad app, I have a UIAlertView which pops up upon start up, however I only want this to popup the very first time the user starts the application. It's a setup prompt, saying, it's your first time, would you like to setup?
How can I do this? I have heard it's best to write out to a plist file and save a bool value, but how would I tackle this?
Modify the following code to suit your needs; you may put it in your root view controller viedDidLoad method. The code keeps track of the first run of the application, of the number of launches and whether or not the your setup prompt has been shown to the user.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]) {
// this is the first run
// store this information
[defaults setObject:[NSDate date] forKey:#"firstRun"];
[defaults setInteger:1 forKey:#"launches"];
[defaults setBool:NO forKey:#"setupPromptHasBeenShown"];
[defaults synchronize];
// now prompt the user to setup the app
// once the the prompt has been shown,
// if the user actually decides to setup the app,
// store this information again, so you will not prompt him/her again
[defaults setBool:YES forKey:#"setupPromptHasBeenShown"];
[defaults synchronize];
}
else{
// this is not the first run
NSInteger daysSinceInstall = [[NSDate date] timeIntervalSinceDate:[defaults objectForKey:#"firstRun"]] / 86400;
NSInteger launches = [defaults integerForKey:#"launches"];
[defaults setInteger:launches+1 forKey:#"launches"];
[defaults synchronize];
}
You could use NSUserDefaults to achieve this with just a few lines of code.
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Related
I have an apps that load a json from a remote server, using a NSURLConnection. Once it has finished loading (calling connectionDidFinishLoading), I want to record if it was the first launch of the application.
So in connectionDidFinishLoading I did that :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL firstLaunch = [defaults boolForKey:#"screen_firstLaunch"];
NSLog(#"First launch : %d",firstLaunch);
[defaults setBool:YES forKey:#"screen_firstLaunch"];
And it displays "First launch : 0" every time ....
So I thought it might be because it's on another thread, so I try to deport the code into a function that I call in connectionDidFinishLoading using performSelectorOnMainThread, but I get the same result ...
Please someone help me, I'm going mad !
Remember to call [defaults synchronize]; after setting your key in the defaults.
I need to track the download of a certain iphone application. I tried a lot and found out that we could track it from the AppStore. But i need to track that from my application itself. So please help me to identify the method that fires when the application starts for the first time. Thanks.
There's no specific method that fires only on the 1st application launch. You can set a flag in user defaults on application start - so if the flag is not present then that will mean that application launched for the 1st time:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
if (![[NSUserDefaults standardDefaults] boolForKey:#"AlreadyLaunched"]){
// First launch logic
[[NSUserDefaults standardDefaults] setBool:YES forKey:#"AlreadyLaunched"];
[[NSUserDefaults standardDefaults] synchronize];
}
...
}
But i need to track that from my application itself.
No.
But if you really want to do this you could use something like this:
BOOL hasUsedSpyWareFunctions = [[NSUserDefaults standardUserDefaults] boolForKey:#"SpyWareKey"];
if (!hasUsedSpyWareFunctions) {
[self spyOnUser];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"SpyWareKey"];
}
if you are a Pro in spying you only set the key to YES if the method returned successfully (ie a network connection could be established)
There’s no such an event, at least not one that I know of. But what you want can be trivially done using NSUserDefaults. Simply check for some boolean flag and if it’s not there, it’s a first run and you can set the flag:
NSString *const AlreadyRunKey = #"already-run";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:AlreadyRunKey]) {
[prefs setBool:YES forKey:AlreadyRunKey];
[prefs synchronize];
// do whatever else you want
}
I have the following code in my initialization (first time) of my app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
Much later in the code (in response to a button press) I check the value using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaults objectForKey:#"init_val"];
initVal is always nil. I have checked and init_val is set up exactly the same in my settings bundle as another field that I can set and read from without issue (they are both set up with a single field named "Key".
#mlewis54:
You can try this code. I am very sure that it will work for you.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
For Retrieving Data You Should Use The Following Code :
NSString *initVal=[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"];
OR
NSString *initVal=[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"];
EDIT:
If still it gives nil, then you can use the following code:
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] valueForKey:#"init_val"]];
OR
NSString *initVal=[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"init_val"]];
Hope this helps you.
Decelare this at an event on click of button ...
NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
[savedData setObject:userEmailTextField.text forKey:#"userid"];
and on load load the data and check the value you have entered is showing or not...
userEmailTextField.text =[[NSUserDefaults standardUserDefaults]objectForKey:#"userid"];
You need to use the line
NSString *initVal = [defaults stringForKey:#"init_val"];
- since it's a String you must use stringForKey. You can similarly use boolForKey to get a boolean value.
There's nothing wrong in your code, i tried to copy it in my project and it worked good,
so your error could be probably when you check initVal...
what's the code you use to see if initVal is nil?
and are you sure that when/where you check initVal, that var/oblect is still visible ?
try to insert this immediately after your reading code:
NSLog(#"____text read in pref: %#", initVal);
it writes this in my console window -> __text read in pref: 1
of course it's an NSString, i hope you keep it in mind it's not an int or a NSNumber when you check it...
luca
It might be that the initialisation isn't happening. Put an NSLog in there to check it's executed.
It works fine for me with the same code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=#"1";
[defaults setObject:uid forKey:#"init_val"];
[defaults synchronize];
NSUserDefaults *defaultss = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaultss objectForKey:#"init_val"];
NSLog(#"Value of initVal: %#",initVal);
O/P on console: Value of initVal: 1
So try to do it with the same code with print on console.
Are you sure you properly initialize NSUserDefaults? When I initialize, in my app delegate, I always create the default user defaults by creating a dictionary of the default values then:
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Where appDefaults is the dictionary. Then you should be able to access them correctly.
I followed the tutorial: http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
And the "Shuffle Switch" (that I just created based on the tutorial) was not in the Settings App. Every time I did an NSLog on the state of the switch, it would return "(null)". The "Slideshow Switch" (which was created the same way) worked fine.
My Settings bundle Root.plist file looks as follows: (copy link and paste into web browser)
i.imgur.com/kb8DT.png
Please help as I need to create, and access a Toggle Switch created in the .plist file. I am new to iPhone Programming.
Here's the code I'm using to set the user preference switch:
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES" forKey:#"ShuffleToggleKey"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
And here's the code I'm using to get the state of the user preference switch:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];
It seems, that you're putting string object and trying to get boolean value.
You should or get out the string like
NSString *enabledStr = [defaults stringForKey:#"ShuffleToggleKey"];
BOOL enabled = [enabledStr boolValue];
or put a boolean value in the first place like that:
[defaults setBool:YES forKey:#"ShuffleToggleKey"];
Then you can retrieve it as
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];
Is there an easy way to have an alert appear only the first time an app is launched, or will I have to do it manually by making a BOOL and having it set to FALSE after it's run the first time?
Sounds easy enough...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"hasBeenLaunchedBefore"]) {
//Show alert
[defaults setBool:YES forKey:#"hasBeenLaunchedBefore"];
[defaults synchronize];
}