Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-05-2010, 01:36 AM   #1 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
ColouredRobot is on a distinguished road
Smile Performing An Action Only the 1st Time Someone Opens Your Ap

I'm wondering if anyone knows how to only perform a certain action the first time someone opens the application. Say you want to display a welcome screen, only the first the the user opens the app, but never after that... how would you do that? Thank you very much!!!
ColouredRobot is offline   Reply With Quote
Old 01-05-2010, 01:39 AM   #2 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default

Hi ColouredRobot!

I don't know if it will do the trick for you, but in your AppDelegate.m method you should have an applicationDidFinishLaunching method, where you can perform actions that only occurs just once the application is loaded for the first time, but never more, so you can just put the code you want here.

Hope this helped!
Johanovski is offline   Reply With Quote
Old 01-05-2010, 01:46 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 70
firebug is on a distinguished road
Default

to do this in my app i used nsuserdefaults in the applicationdidfinishlaunching or whatever method, it looked something like this
Code:
applicationDidFinishLaunching{

    BOOL firstlaunch = [[NSUserDefaults standardUserDefault] boolForKey:@"somerandomkey"];
    if(firstlaunch==NO){
          //do whatever
          [[NSUserDefaults standardUserDefault] setBool: YES ForKey@"somerandomkey"];
    }
}
not on my mac atm so i cant see the correct capitalization/syntax and whatnot, but the first time the app launches it will check for a key on the device, since its the first time it wont be able to find it so it will return NO. at the end of whatever you do for the first launch set the value for that key to yes and the code wont run again.
firebug is offline   Reply With Quote
Old 01-08-2010, 04:48 AM   #4 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
ColouredRobot is on a distinguished road
Default

Quote:
Originally Posted by firebug View Post
to do this in my app i used nsuserdefaults in the applicationdidfinishlaunching or whatever method, it looked something like this
Code:
applicationDidFinishLaunching{

    BOOL firstlaunch = [[NSUserDefaults standardUserDefault] boolForKey:@"somerandomkey"];
    if(firstlaunch==NO){
          //do whatever
          [[NSUserDefaults standardUserDefault] setBool: YES ForKey@"somerandomkey"];
    }
}
not on my mac atm so i cant see the correct capitalization/syntax and whatnot, but the first time the app launches it will check for a key on the device, since its the first time it wont be able to find it so it will return NO. at the end of whatever you do for the first launch set the value for that key to yes and the code wont run again.
Thanks for the example, but just to clarify, say, I only want an alert to display the first time. Would the "do whatever" area be where I'd write the code I want implemented only on the first launch?
Thanks!
ColouredRobot is offline   Reply With Quote
Old 01-08-2010, 09:57 AM   #5 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
mr-sk is on a distinguished road
Default

Quote:
Originally Posted by ColouredRobot View Post
Thanks for the example, but just to clarify, say, I only want an alert to display the first time. Would the "do whatever" area be where I'd write the code I want implemented only on the first launch?
Thanks!
Yes, that is where you would do your first time operation. Just make sure you update the UserDefaults so it doesn't happen on subsequent launches:

Code:
          [[NSUserDefaults standardUserDefault] setBool: YES ForKey@"somerandomkey"];
mr-sk is offline   Reply With Quote
Old 01-08-2010, 05:14 PM   #6 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
ColouredRobot is on a distinguished road
Default

Quote:
Originally Posted by mr-sk View Post
Yes, that is where you would do your first time operation. Just make sure you update the UserDefaults so it doesn't happen on subsequent launches:

Code:
          [[NSUserDefaults standardUserDefault] setBool: YES ForKey@"somerandomkey"];
COOL! Awesome,one more thing, can I also do it backwards? Say my normal code is what displays what I'd like the user to see the first time, do I then switch the if command to:

Code:
if(firstlaunch==YES)
to display the screen I'd like the user to see from there and after?

Thank you soooo much!
ColouredRobot is offline   Reply With Quote
Old 01-08-2010, 05:19 PM   #7 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
mr-sk is on a distinguished road
Default

Sure:

Code:
if(firstLaunch == YES)
{
     // Do first launch stuff (only happens once)
    firstLaunch = NO;
}
else
{
    // Do always stuff
}
mr-sk is offline   Reply With Quote
Old 01-08-2010, 10:23 PM   #8 (permalink)
College Student
 
ColouredRobot's Avatar
 
Join Date: Dec 2009
Location: Los Angeles, CA
Posts: 75
ColouredRobot is on a distinguished road
Default

Quote:
Originally Posted by mr-sk View Post
Sure:

Code:
if(firstLaunch == YES)
{
     // Do first launch stuff (only happens once)
    firstLaunch = NO;
}
else
{
    // Do always stuff
}
Amazing! Thanks, but could I potentially also use this in a view controller (randomViewController.m) like in ViewDidLoad?
ColouredRobot is offline   Reply With Quote
Old 01-08-2010, 10:39 PM   #9 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 70
firebug is on a distinguished road
Default

Quote:
Originally Posted by ColouredRobot View Post
Amazing! Thanks, but could I potentially also use this in a view controller (randomViewController.m) like in ViewDidLoad?
yup, do everything the same as before but use a different key like:

Code:
viewDidLoad{

    BOOL firstlaunch = [[NSUserDefaults standardUserDefault] boolForKey:@"someotherrandomkey"];
    if(firstlaunch==NO){
          //do whatever
          [[NSUserDefaults standardUserDefault] setBool: YES ForKey@"someotherrandomkey"];
    }
    if(firstLaunch == YES)
    {
     // Do first launch stuff (only happens once)
    firstLaunch = NO;
    }
    else
    {
    // Do always stuff
    }
}
firebug is offline   Reply With Quote
Reply

Bookmarks

Tags
action, first time, one time, time

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
16 members and 328 guests
akacaj, alexP, ClerurcifeDer, Domele, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, michelle, NetGuru, NSString, Paul Slocum, Punkjumper, Sloshmonster, soohyun
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:51 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0