How to Show popup Alert view when apps run first time? - iphone

Everyone, I'm working in iphone. I've a problem that I want to show a alert view popup when my apps run first time in iphone. I'm not getting how to do this. Please someone help me.

BOOL foo = [[NSUserDefaults standardUserDefaults]boolForKey:#"previouslyLaunched"];
if (!foo)
{
NSLog(#"FirstLaunch");
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"previouslyLaunched"];
}
Edit: What Akshay said earlier, with some code.

3 simple lines
-(BOOL)application:(UIApplication *)application … {
if(![[[NSUserDefaults standardUserDefaults] objectForKey:#"NOT_FIRST_TIME"] boolValue])
{
[[[[UIAlertView alloc] initWithTitle:nil message:#"message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] autorelease] show];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:#"NOT_FIRST_TIME"];
}
}

You will probably need to store some data on first run of the application (like current date). Every time the application starts you will check for this data and if it doesn't exist store it and display the popup.

Put the alertview code in didFinishLaunchingWithOptions function in delegate.m

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"FirstTime"] == nil)
{
//your alert message
}
[defaults setObject:#"firsttime" forKey:#"FirstTime"];

Save a BOOL value using NSUserDefaults which corresponds to whether it is the application's first launch. If you find the boolean to be YES (or don't find the value), display the alert and set the value to NO.

Related

Showing an UIAlertView the first time the app is launched only

Am I doing something wrong in getting an UIAlertView to play the first time only? In my didFinishLaunchingWithOptions, my MainViewController gets instantiated. So in MainViewController's viewDidLoad, I do this:
BOOL shouldAlert = [[NSUserDefaults standardUserDefaults] boolForKey:#"ShowAlert"];
if (!shouldAlert) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyAlert" message:#"Some text here" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles: nil];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"ShowAlert"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
When I try it on the device, it doesn't work if I double tap home, and hit the minus sign next to the app to remove it from the background mode (I think that's what double tapping does right?). Cause after I do that, the pop up appears again. If I don't do that, than the alert only shows once. Is this expected behavior? Thanks a bunch.
you get your shouldAlert first time from userdefauls?
I think you should check if the userdefaults exists else your bool is NO or FALSE
check this example:
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
shouldAlert = YES; // define in the .h
if(userDef)
shouldAlert = [userDef boolForKey:#"ShowAlert"];
Try with an integer (0 / 1). I had some problems too with BOOL.
setInteger:forKey:
integerForKey:
EDIT
Try updating the defaults before initing/showing the alert.
And please, update your variable names. Your code is a nonsense with their current name.
Try this :
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
BOOL hasAlreadyBeenLaunched = [defaults boolForKey:#"HasAlreadyBeenLaunched"];
if (!hasAlreadyBeenLaunched) {
[defaults setBool:YES forKey:#"HasAlreadyBeenLaunched"];
[defaults synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyAlert" message:#"Some text here" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles: nil];
[alert show];
[alert release];
}

how to show popup every 3rd time app has launched?

I need to show a popup every 3rd time my app has lunached.
I am using Appirater also for rating my app.Will it be ok if i append code in that to get my task done?
or is there any other way to acknowledge launching of my app every 3rd time?
Here you can store in NSUserDefault for the App launch count and can show the alert every third time app launching.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(![[[NSUserDefaults standardUserDefaults] valueForKey:#"firstTime"] isEqualToString:#"Yes"])
{
[[NSUserDefaults standardUserDefaults] setValue:#"Yes" forKey:#"firstTime"];
[[NSUserDefaults standardUserDefaults] setInteger:([[NSUserDefaults standardUserDefaults] integerForKey:#"ApplaunchCount"] + 1) forKey:#"ApplaunchCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults] setInteger:([[NSUserDefaults standardUserDefaults] integerForKey:#"ApplaunchCount"] + 1) forKey:#"ApplaunchCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
if([[NSUserDefaults standardUserDefaults] integerForKey:#"ApplaunchCount"] % 3 ==0)
{
UIAlertView *lanuchAlert = [[UIAlertView alloc] initWithTitle:#"Your Message Title" message:#"Your Message Text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[lanuchAlert show];
[lanuchAlert release];
}
}
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Let me know if you need any more help.
You can adjust LAUNCHES_UNTIL_PROMPT in "Appirater.h". Or you can use userDefaults.
I don't know about Appirater, but you can use NSUserDefaults and a simple logic to achieve this. Set an NSNumber value to user defaults and increment it every time the app loads. Initially the number will be nil when you try to access it using the key. If it is nil set 1 as the value, and increment it every time the app loads. If the number becomes 3, show the pop-up and reset the number to 0.

One time UIAlert?

I want to do a UIalert the first time a user opens my app, to tell them about basic functionality, but I don't want it showing up every time they open the app or reload the home screen. I've seen this in other apps but don't know what the best way of going about it would be. If anyone could point me in the right direction on this it would be appreciated.
Stick this into you App Delegate in the - (void)applicationDidFinishLaunching:(UIApplication )application method
BOOL hasRunBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"FirstRun"];
if (!hasRunBefore) {
UIAlertView *firstRun = [[UIAlertView alloc] initWithTitle:#"Virgin" message:#"Hello World!!" delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[firstRun show];
[firstRun release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstRun"];
}
else if (hasRunBefore) {
//can do some else if run before
}
Basically what this does is check for the key "FirstRun" in NSUserDefaults, similarly this could be modified to be a version number or something, say if you wanted a new alert after each update or something. It then checks if the key does not else (!hasRunBefore) and if it does not exist (so never been run before) it creates a UIAlertView and then sets YES for the key (also creating that ket in process)
Use "[NSUserDefaults standardUserDefaults]" to write a boolean if they've started the app before or not.
I use NSUserDefaults in the viewDidLoad method of my main screen. So every time the app starts and the main screen loads, it checks if its the user's first time.
This is how I do it in my app:
- (void)viewDidLoad {
BOOL tempBOOL = [[NSUserDefaults standardUserDefaults] boolForKey:#"hasSeenOpeningAlert"];
if (!tempBOOL) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Welcome To My App" message:#"This app will ... First you need to ..." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
[super viewDidLoad];
}
and then:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
Edit *editViewController = [[[Edit alloc] initWithNibName:#"Edit" bundle:nil]retain];
[self.navigationController presentModalViewController:editViewController animated:YES];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasSeenOpeningAlert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[editViewController release];
}

NSUserDefaults problem

I have this in my app delegate applicationDidFinishLaunching method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"AcceptTC"] == nil){
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"NO" forKey:#"AcceptTC"];
[defaults registerDefaults:appDefaults];
}
and I have this in my RootViewController viewDidLoad method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"AcceptTC"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notice" message:#"By using this application you agree to be bound by the Terms and Conditions as stated within this application." delegate:self cancelButtonTitle:#"No Deal" otherButtonTitles:#"I Understand",nil];
[alert show];
[alert release];
}
and my alert view delegate does this:
if(buttonIndex == 0){
exit(0);
}
else{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"YES" forKey:#"AcceptTC"];
}
However when I click "I understand" (button index 1) and then restart the application I still see the alert view! Even though I've definiely set the value to YES.
I have no idea how to change this. :( I only want it to show the first time a user starts the application - i don't want to keep showing it every time they want to use it.
Thanks
In my application I'm using NSUserDefaults with a bool, works fine.
When the first ViewController loads, it will do:
BOOL terms = [[NSUserDefaults standardUserDefaults] boolForKey:#"termsaccepted"];
if (!terms) {
[self presentModalViewController:disclaimerViewController animated:YES];
}
Within the disclaimer view, after the button has been tapped:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"termsaccepted"];
[[NSUserDefaults standardUserDefaults] synchronize];
I think you're missing the "synchronize" part. However I find using a bool more streamlined, too.
Maybe you need to call synchronize on the defaults to save the changes to disk?
Concering registerDefaults:
The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.
// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary \
dictionaryWithContentsOfFile:[[NSBundle mainBundle] \
pathForResource:#"Defaults" ofType:#"plist"]]];
Code taken from this SO answer.
Another blog article about NSDefaults:
http://retrodreamer.com/blog/2010/07/slight-change-of-plan/

Calling a method only on the initial launch of an application

How do you call a method on only the initial launch of an application? For example, if you wanted to put a tutorial UIAlertView on the very first launch but never again after that?
Easy. When your app launches, check [NSUserDefaults standardUserDefaults] for the presence of a bool that you put in there. If the boolean isn't there (or it's not YES), then show the alert and save the boolean back into NSUserDefaults as YES.
It's all of about 4 lines of code:
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasShownInitialAlert"] == NO) {
UIAlertView * alert = [[UIAlertView alloc] init...];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasShownInitialAlert"];
}
Use the NSUserDefaults for this. Store a BOOL there saying if its firstlaunch, and set it in applicationDidFinishLaunching. Then then only time that is false is after fresh installs of the app.