简体   繁体   English

应用启动后如何运行一次?

[英]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 我忘了提到这是我的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];
    }
}

NSUserDefaults does not commit the changes to disk until you send the synchronize message. 在发送synchronize消息之前, NSUserDefaults不会将更改提交到磁盘。

Try adding doing this: 尝试添加这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM