iOS经典讲解之判断App是否第一次启动

时间:2021-03-30 16:36:53

作者:Loving_iOS

转载请标明出处:http://blog.csdn.net/loving_ios/article/details/50435840


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

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
//第一次启动
}else{
//不是第一次启动了
}

方法二、判断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;
}