对于是否为首次安装的App可以使用如下方法来判断
[[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
如果App是通过App Store升级的话可以通过版本号来判断
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
实际使用可以在didFinishLaunchingWithOptions:中添加如下代码
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"]; NSLog(@"安装之后首次开启App"); //获取当前App版本号并存入NSUserDefaults中 NSString* appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:appVersion forKey:@"appVersion"]; [userDefaults synchronize]; } else { //根据版本号判断是否更新后首次启动 NSString* appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString* localVersion = [userDefaults stringForKey:@"appVersion"]; if ([appVersion isEqualToString:localVersion]) { NSLog(@"安装或升级之后并非首次开启App"); } else { NSLog(@"升级之后首次开启App"); //将当前App版本号存入NSUserDefaults中 [userDefaults setObject:appVersion forKey:@"appVersion"]; [userDefaults synchronize]; } }
时间: 2024-10-07 01:32:16