Reputation: 1401
i create an apps that i want to prompt an message for the user.. maybe using UIAlertview. After that, if the user run the apps for the second time, the alert won't prompt up anymore.
is that possible? honestly i don't have any idea about how to doing this. Any idea? I search on STO, actually this link, still confused.
what is NSUserDefaults? How can NSUserDefaults store this information? i mean this is my first time or second time.
thanks.
Upvotes: 2
Views: 3181
Reputation: 3696
To know what's NSUserDefaults, I suggest to take a look the official doc.
And of course you can use it to fulfill your goal. You use a user default to store information about the current amount of runs in the app.
More or less like:
BOOL isRunMoreThanOnce = [[NSUserDefaults standardUserDefaults] boolForKey:@"isRunMoreThanOnce"];
if(!isRunMoreThanOnce){
// Show the alert view
// Then set the first run flag
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isRunMoreThanOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Upvotes: 7
Reputation: 2205
Yes, U can save a value in NSUserDefault for the first time in your app & set it some other value once you open the app.
like
if(![[NSUserDefault standardUserDefault] objectforKey:@"AppOpenFirstTime"])
{
// App Open First time
// Show Alert
[[NSUserDefault standardUserDefault] setObject:@"1" forKey:@"AppOpenFirstTime"]
}
Upvotes: 3
Reputation: 1123
You can check if you stored some value in NSUserDefaults
NSString *flag = [[NSUserDefaults standardUserDefaults] stringForKey:@"not_first_run"];
if (!flag) {
//first run, do somethig
}
and then set it to some value
[[NSUserDefaults standardUserDefaults] setObject:@"just any string" forKey:@"not_first_run"];
NSUserDefaults is a key-value store saved between your application launches.
Upvotes: 2
Reputation: 4329
First Time when your application launch at that time boolForKey:@"AlreadyRan"
is FALSE
. after that set it TRUE
.
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"AlreadyRan"] )
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"AlreadyRan"];
}
With the NSUserDefaults class, you can save settings and properties related to application or user data.
The objects will be saved in what is known as the iOS “defaults system”. The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions.This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app!
Upvotes: 1