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
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
}
}