iOS小技巧13- 判断iOS App程序安装后第一次启动,更新后第一次启动
1、判断程序第一次启动
/*
NSUserDefaults 全局的--保存到Preferences 存数据比较少,可以用来传值
适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类。
NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。
*/
NSUserDefaults *myUD=[NSUserDefaults standardUserDefaults];
if(![myUD boolForKey:@"firstStart"])
{
[myUD setBool:YES forKey:@"firstStart"];
[myUD synchronize];//同步
NSLog(@"第一次启动");
}else{
NSLog(@"不是第一次启动");
}
2、判断程序是否更新后第一次启动
- (BOOL) isAppFirstRun{
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"CFBundleShortVersionString"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastRunKey = [defaults objectForKey:@"last_run_version_key"];
if (!lastRunKey) {
[defaults setObject:currentVersion forKey:@"last_run_version_key"];
return YES;
// App is being run for first time
//上次运行版本为空,说明程序第一次运行
}
else if (![lastRunKey isEqualToString:currentVersion]) {
[defaults setObject:currentVersion forKey:LAST_RUN_VERSION_KEY];
return YES;
// App has been updated since last run
//有版本号,但是和当前版本号不同,说明程序已经更新了版本
}
return NO;
}
[[NSBundle mainBundle] infoDictionary]的内容
/*
[[[NSBundle mainBundle] infoDictionary]={
BuildMachineOSBuild = 14F27;
CFBundleDevelopmentRegion = en;
CFBundleExecutable = firstRunApp;
CFBundleIdentifier = "LotusNing.firstRunApp";
CFBundleInfoDictionaryVersion = "6.0";
CFBundleInfoPlistURL = "Info.plist -- file:///Users/l/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Bundle/Application/BEC096F3-10C3-4BFB-BC75-03C35289BB9F/firstRunApp.app/";
CFBundleName = firstRunApp;
CFBundleNumericVersion = 16809984;
CFBundlePackageType = APPL;
CFBundleShortVersionString = "1.0";
CFBundleSignature = "????";
CFBundleSupportedPlatforms = (
iPhoneSimulator
);
CFBundleVersion = 1;
DTCompiler = "com.apple.compilers.llvm.clang.1_0";
DTPlatformBuild = "";
DTPlatformName = iphonesimulator;
DTPlatformVersion = "9.0";
DTSDKBuild = 13A340;
DTSDKName = "iphonesimulator9.0";
DTXcode = 0701;
DTXcodeBuild = 7A1001;
LSRequiresIPhoneOS = 1;
MinimumOSVersion = "9.0";
UIDeviceFamily = (
1,
2
);
UILaunchStoryboardName = LaunchScreen;
UIMainStoryboardFile = Main;
UIRequiredDeviceCapabilities = (
armv7
);
UISupportedInterfaceOrientations = (
UIInterfaceOrientationPortrait,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight
);
}
*/