计算应用程序在 xcode 中打开的次数

Sos*_*ily 3 iphone xcode objective-c

我想知道是否有人知道我如何计算我的应用程序打开的次数。NSUserDefalte 什么的...我应该把 var 放在哪里,它应该在哪里初始化为 0?

Ash*_*bay 5

在你的类 AppDelegate.m 你可以这样做:

//Application did launch
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}

//The application was in background and become active
- (void)applicationWillEnterForeground:(UIApplication *)application
{
  int count = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchCount"];
  if(count < 0) count = 0;
  [[NSUserDefaults standardUserDefaults] setInteger:count+1 forKey:@"LaunchCount"];
}
Run Code Online (Sandbox Code Playgroud)