Thursday, July 22, 2010

Show Popup when Application is installed for First time - Iphone

Hi All,

You might have seen the applications in I-phone that shows a welcome message only when you install the application for instance you might think that there is a Database at back end which is used to save some constants but most apps are without any database.

The most interesting way that i found in getting a popup message on application install is using NSUserDefaults this contains a set of constants that are related to a particular user. so what we will do is that when ever the user launches application for the first time we will add a new constant in NsUserdefault and from then on every time we will check the value of it so for every next time the value will be saved with constant name and hence no action will be taken.

Let me share a code snippet with you.

 1 - (void)viewDidLoad {
 2  int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"CONSTANT_NAME"];
 3  if( x == 1 )
 4  {} 
 5  else
 6  {
 7   [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"CONSTANT_NAME"];
 8   [[NSUserDefaults standardUserDefaults]synchronize]; 
 9   
10   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@
"Thank you for installing the iPhone application."
 delegate:self cancelButtonTitle:@"Later" otherButtonTitles:@"Okay" ,nil];
11   [alert show];
12   [alert release];
13  }
14     [super viewDidLoad];
15 }



Line 1: The function viewdidLoad will be called when the View controller is loaded.
Line 2: The first thing it will do is to check the constants name and save its value to a variable.

Line 3: A conditional statement will check if the value exists in constant.
Line 7: Save some value in NsUserDefault.
Line 10: Show an alert view as the constant is not set.

Happy coding.

No comments: