2010-09-21 71 views
0

在我的iPad應用程序中,我有一個UIAlertView,它在啓動時彈出,但是我只想讓它在用戶第一次啓動應用程序時彈出。它的設置提示,說,你第一次,你想設置?第一次打開事件

我該怎麼做?我已經聽到最好寫出一個plist文件,並保存一個布爾值,但我將如何解決這個問題?

謝謝。

回答

3

修改下面的代碼,以滿足您的需求;你可以把它放在你的根視圖控制器的viedDidLoad方法中。該代碼會跟蹤應用程序的第一次運行,啓動次數以及您的設置提示是否已顯示給用戶。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults objectForKey:@"firstRun"]) { 

    // this is the first run 
    // store this information 

    [defaults setObject:[NSDate date] forKey:@"firstRun"]; 
    [defaults setInteger:1 forKey:@"launches"]; 
    [defaults setBool:NO forKey:@"setupPromptHasBeenShown"]; 
    [defaults synchronize]; 

    // now prompt the user to setup the app 
    // once the the prompt has been shown, 
    // if the user actually decides to setup the app, 
    // store this information again, so you will not prompt him/her again 
    [defaults setBool:YES forKey:@"setupPromptHasBeenShown"]; 
    [defaults synchronize]; 

} 
else{ 
    // this is not the first run 
    NSInteger daysSinceInstall = [[NSDate date] timeIntervalSinceDate:[defaults objectForKey:@"firstRun"]]/86400; 
    NSInteger launches = [defaults integerForKey:@"launches"]; 
    [defaults setInteger:launches+1 forKey:@"launches"]; 
    [defaults synchronize]; 

}