检查我的IOS应用程序是否更新

前端之家收集整理的这篇文章主要介绍了检查我的IOS应用程序是否更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要检查我的应用程序启动时是否正在更新,因为我需要
以使得只有当应用程序首次安装才能再次出现才会出现
更新.

解决方法

您可以将值(例如当前应用程序版本号)保存到NSUserDefaults,并在每次用户启动应用程序时进行检查.
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. // ...
  4.  
  5. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  6.  
  7. NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  8. NSString *prevIoUsVersion = [defaults objectForKey:@"appVersion"];
  9. if (!prevIoUsVersion) {
  10. // first launch
  11.  
  12. // ...
  13.  
  14. [defaults setObject:currentAppVersion forKey:@"appVersion"];
  15. [defaults synchronize];
  16. } else if ([prevIoUsVersion isEqualToString:currentAppVersion]) {
  17. // same version
  18. } else {
  19. // other version
  20.  
  21. // ...
  22.  
  23. [defaults setObject:currentAppVersion forKey:@"appVersion"];
  24. [defaults synchronize];
  25. }
  26.  
  27.  
  28.  
  29. return YES;
  30. }

swift版本看起来像这样:

  1. let defaults = NSUserDefaults.standardUserDefaults()
  2.  
  3. let currentAppVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
  4. let prevIoUsVersion = defaults.stringForKey("appVersion")
  5. if prevIoUsVersion == nil {
  6. // first launch
  7. defaults.setObject(currentAppVersion,forKey: "appVersion")
  8. defaults.synchronize()
  9. } else if prevIoUsVersion == currentAppVersion {
  10. // same version
  11. } else {
  12. // other version
  13. defaults.setObject(currentAppVersion,forKey: "appVersion")
  14. defaults.synchronize()
  15. }

猜你在找的iOS相关文章