我如何检测第三次应用程序启动

时间:2014-03-19 08:21:51

标签: ios iphone objective-c ios7

我希望在我的应用程序第三次启动时显示警报视图。我到处搜索但无法找到解决方案。我怎么知道我的应用程序已经第三次启动了?

先谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用这些方法轻松读取和写入文件

+ (void) setSetting: (NSString *)key value: (NSString *)value {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:value forKey:[NSString stringWithFormat:@"%@", key]]; 
[defaults synchronize];
}

+ (NSString *) getSetting: (NSString *)key defaultVal: (NSString *)defaultVal {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *setting = [defaults stringForKey:[NSString stringWithFormat:@"%@", key]];
if (setting == nil) {
    setting = defaultVal;
}

return setting;
}

每次触发appDelegate中的didFinishLaunchingWithOptions:时,您都可以更新存储在用户默认值中的值

答案 1 :(得分:2)

这很容易。在NSUserDefaults中保存NSNumber,并在每次启动应用程序时增加它。当它命中3然后显示警报。

答案 2 :(得分:1)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
// ...
if ([self plusPlusLaunchCount] == 3) {
    [self showRateUsAlert];
}
return YES;
}

- (void)showRateUsAlert {
// show the Rate Us alert view
}

- (NSInteger)plusPlusLaunchCount {
static NSString *Key = @"launchCount";
NSInteger count = 1 + [[NSUserDefaults standardUserDefaults] integerForKey:Key];
[[NSUserDefaults standardUserDefaults] setInteger:count forKey:Key];
return count;
}