应用程序启动时如何运行一次?

时间:2011-06-15 16:29:39

标签: iphone objective-c cocoa-touch ipad

当我的应用程序正在启动时,我正在尝试播放一个介绍电影,但我已经完全疯了,我在我的代码中做了很多测试,并试图使用这个

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

1 个答案:

答案 0 :(得分:1)

在您发送NSUserDefaults消息之前,

synchronize不会将更改提交到磁盘。

尝试添加这样做:

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