Ahmed
Ahmed

Reputation: 11

How to run something once when the app is launched?

I'm trying to play an intro movie when my app is getting launch, but am totally driven crazy already, I did a lot of testing in my code, along with trying to use this

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     // Override point for customization after application launch.
     NSLog(@"App started to launch");
     [self.window makeKeyAndVisible];
     [self.window addSubview:_intro.view];
     return YES;
 }

but that's make my video run, even if I'm coming from the background.
If like pressing the middle button, then double pressing the middle button and pressing app icon, I get the movie to play again.

I forget to mention that this is my ViewDidLoad

- (void)viewDidLoad    
{
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL launchedBefore = [userDefaults boolForKey:@"hasRunBefore"];
    NSLog(@"value of hasRunBefore is %d",launchedBefore);
    if(!launchedBefore)
    {
        [userDefaults setBool:1 forKey:@"hasRunBefore"];
        launchedBefore = [userDefaults boolForKey:@"hasRunBefore"];   
        NSLog(@"value of hasRunBefore is %d",launchedBefore);
        [self playvideo];
    }
}

Upvotes: 1

Views: 173

Answers (1)

Martin Adoue
Martin Adoue

Reputation: 66

NSUserDefaults does not commit the changes to disk until you send the synchronize message.

Try adding doing this:

[userDefaults setBool:YES forKey:@"hasRunBefore"];
[userDefaults synchronize];

Upvotes: 1

Related Questions