如何获取应用程序启动iPhone的次数

时间:2011-10-20 08:45:30

标签: iphone objective-c

我正在开发一个提醒应用。

所以我的客户想要设置此应用程序弹出消息的速率,这将在用户打开应用程序的第10次出现。这可能。

我该如何实现?

任何人都可以帮助我。谢谢提前

4 个答案:

答案 0 :(得分:6)

您可以使用NSUserDefaults:

    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger appLaunchAmounts = [userDefaults integerForKey:@"LaunchAmounts"];
    if (appLaunchAmounts == 10)
    {
        [self showMessage];
    }
    [userDefaults setInteger:appLaunchAmounts+1 forKey:@"LaunchAmounts"];

答案 1 :(得分:5)

您可以将其存储到NSUserDefaults中。只需在applicationDidFinishLaunching:中进行更新。

答案 2 :(得分:4)

您可以在NSUserDefaults

中保存一个整数
- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName

每次调用appDidFinishLaunching(或appWillEnterForeground)委托方法时,检索并递增它。可能最好使用appWillEnterForeground,因为有时应用程序可能会在未终止的情况下存在于背景中。

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSInteger count = [defaults integerForKey:@"LaunchCount"];
count++;
/* Do checks and review prompt */
[defaults setInteger:count forKey:@"LaunchCount"];
[defaults synchronize];

答案 3 :(得分:1)

这将在NSUserDefaults中存储一个名为“AppLaunchCount”的值。

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


if ([[NSUserDefaults standardUserDefaults] integerForKey:@"AppLaunchCount"])
{
    [[NSUserDefaults standardUserDefaults] setInteger:([[NSUserDefaults standardUserDefaults] integerForKey:@"AppLaunchCount"] + 1) forKey:@"AppLaunchCount"];
}
else
{
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"AppLaunchCount"];
}
}