Check if my IOS application is updated

前端 未结 8 1094
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 18:07

I need to check when my app launches if it was being updated, because i need to make a view that only appears when the app is firstly installed to appear again after being

8条回答
  •  无人及你
    2020-12-05 18:41

    Just initialise AppVersionUpdateNotifier in app launch and conform AppUpdateNotifier protocol, enjoy.

    Use: Swift 3.x

    extension AppDelegate: AppUpdateNotifier {
        func onVersionUpdate(newVersion: Int, oldVersion: Int) {
            // do something
        }
        
        func onFirstLaunch() {
            //do something
        }
    }
    
        class AppVersionUpdateNotifier {
            static let KEY_APP_VERSION = "key_app_version"
            static let shared = AppVersionUpdateNotifier()
            
            private let userDefault:UserDefaults
            private var delegate:AppUpdateNotifier?
            
            private init() {
                self.userDefault = UserDefaults.standard
            }
            
            func initNotifier(_ delegate:AppUpdateNotifier) {
                self.delegate = delegate
                checkVersionAndNotify()
            }
            
            private func checkVersionAndNotify() {
                let versionOfLastRun = userDefault.object(forKey: AppVersionUpdateNotifier.KEY_APP_VERSION) as? Int
                let currentVersion = Int(Bundle.main.buildVersion)!
                
                if versionOfLastRun == nil {
                    // First start after installing the app
                    delegate?.onFirstLaunch()
                } else if versionOfLastRun != currentVersion {
                    // App was updated since last run
                    delegate?.onVersionUpdate(newVersion: currentVersion, oldVersion: versionOfLastRun!)
                } else {
                    // nothing changed
                    
                }
                userDefault.set(currentVersion, forKey: AppVersionUpdateNotifier.KEY_APP_VERSION)
            }
        }
        
        protocol AppUpdateNotifier {
            func onFirstLaunch()
            func onVersionUpdate(newVersion:Int, oldVersion:Int)
        }
        extension Bundle {
            var shortVersion: String {
                return infoDictionary!["CFBundleShortVersionString"] as! String
            }
            var buildVersion: String {
                return infoDictionary!["CFBundleVersion"] as! String
            }
        }
    

提交回复
热议问题