My application has a notification section in it, and the sum of the notifications are displayed in the form of Badge Count on the app icon. When the user accesses the notifi
Run freshInstallationCheck function in didFinishLaunchingWithOptions section.
func freshInstallationCheck() {
let defaults = UserDefaults.standard
guard let currentAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else { return }
guard let previousVersion = defaults.string(forKey: "appVersion") else {
// Key does not exist in UserDefaults, must be a fresh install
print("fresh install")
// Writing version to UserDefaults for the first time
defaults.set(currentAppVersion, forKey: "appVersion")
// reinstall application, force to set icon to zero
UIApplication.shared.applicationIconBadgeNumber = 0
return
}
let comparisonResult = currentAppVersion.compare(previousVersion, options: .numeric, range: nil, locale: nil)
switch comparisonResult {
case .orderedSame:
// nothing to do
break
case .orderedAscending, .orderedDescending:
// new version update or downgrade
break
}
// Updating new version to UserDefaults
defaults.set(currentAppVersion, forKey: "appVersion")
}