Badge count is persisted after deleting an app and installing it again

前端 未结 4 2249
猫巷女王i
猫巷女王i 2020-12-18 20:38

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

4条回答
  •  时光取名叫无心
    2020-12-18 21:01

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

提交回复
热议问题