iOS 判断App是否第一次启动

时间:2021-11-03 16:37:14

方法一、在didFinishLaunchingWithOptions方法中加入如下代码:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){  
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
//第一次启动
}else{
//不是第一次启动了
}
此处测试可以通过

[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];


方法二、判断app是否第一次启动或者更新后第一次启动

#define LAST_RUN_VERSION_KEY @"last_run_version_of_application"  
- (BOOL) isFirstLoad{
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"CFBundleShortVersionString"];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *lastRunVersion = [defaults objectForKey:LAST_RUN_VERSION_KEY];

if (!lastRunVersion) {
[defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
return YES;
}
else if (![lastRunVersion isEqualToString:currentVersion]) {
[defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
return YES;
}
return NO;
}

此处测试可以通过

改变bundle version

iOS 判断App是否第一次启动