我需要检查我的应用程序启动时是否正在更新,因为我需要
以使得只有当应用程序首次安装才能再次出现才会出现
更新.
以使得只有当应用程序首次安装才能再次出现才会出现
更新.
解决方法
您可以将值(例如当前应用程序版本号)保存到NSUserDefaults,并在每次用户启动应用程序时进行检查.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; NSString *prevIoUsVersion = [defaults objectForKey:@"appVersion"]; if (!prevIoUsVersion) { // first launch // ... [defaults setObject:currentAppVersion forKey:@"appVersion"]; [defaults synchronize]; } else if ([prevIoUsVersion isEqualToString:currentAppVersion]) { // same version } else { // other version // ... [defaults setObject:currentAppVersion forKey:@"appVersion"]; [defaults synchronize]; } return YES; }
swift版本看起来像这样:
let defaults = NSUserDefaults.standardUserDefaults() let currentAppVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String let prevIoUsVersion = defaults.stringForKey("appVersion") if prevIoUsVersion == nil { // first launch defaults.setObject(currentAppVersion,forKey: "appVersion") defaults.synchronize() } else if prevIoUsVersion == currentAppVersion { // same version } else { // other version defaults.setObject(currentAppVersion,forKey: "appVersion") defaults.synchronize() }