簡體   English   中英

應用啟動后如何運行一次?

[英]How to run something once when the app is launched?

我想在我的應用啟動時播放一部介紹性電影,但已經完全瘋了,我在代碼中做了很多測試,並嘗試使用它

 - (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;
 }

但這使我的視頻得以播放,即使我來自背景。
如果喜歡按中間按鈕,然后再按兩次中間按鈕並按應用程序圖標,我會再次播放電影。

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

在發送synchronize消息之前, NSUserDefaults不會將更改提交到磁盤。

嘗試添加這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM