Check if my IOS application is updated

前端 未结 8 1098
爱一瞬间的悲伤
爱一瞬间的悲伤 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:46

    I think, given answers are good when you've a small scale application but when you working on a large-scale iOS apps with a long roadmap you definitely need a strong futuristic solution with benefits like.

    1. Better compare logic that works fine with all possibilities (2.0/2.0.0 etc).
    2. Take specific action during migration, assume when user migrating from version 1.0 to 2.0 verses 1.1 to 2.0.
    3. Reset the cached version when the user reset the app (Logout).

    Below is the code snippet that I used in one of my iOS App.

    public enum VersionConfigResponse {
        case freshInstalled
        case versionGreater
        case versionEqualOrLesser
        case currentVersionEmpty
    }
    
    
    /*
     Config manager responsible to handle the change needs to be done when user migrate from one Version to another Version.
     */
    public class VersionConfig {
    
        public static let shared = VersionConfig()
        private init() { }
        private let versionKey = "SAVED_SHORT_VERSION_STRING"
    
        /*
         Cache the existing version for compare during next app launch.
         */
        private var storedVersion : String?  {
            get {
                return UserDefaults.standard.object(forKey: versionKey) as? String
            } set {
                UserDefaults.standard.set(newValue, forKey: versionKey)
                UserDefaults.standard.synchronize()
            }
        }
    
        /*
         Validate the current version with saved version, if greater do clean.
         */
        public func validate(currentVersion: String?) -> VersionConfigResponse  {
            guard let currentVersion = currentVersion else {
                return .currentVersionEmpty
            }
    
            guard let sVersion = storedVersion else {
                self.storedVersion = currentVersion
                self.freshInstalled()
                return .freshInstalled
            }
    
            if currentVersion.compare(sVersion, options: .numeric, range: nil, locale: nil) == .orderedDescending    {
                self.storedVersion = currentVersion
                self.userMigrated(fromVersion: sVersion, toVersion:currentVersion)
                return .versionGreater
            } else {
                return .versionEqualOrLesser
            }
        }
    
        private func userMigrated(fromVersion: String, toVersion: String)   {
            //take your action here...
        }
    
        private func freshInstalled()   {
            //take your action here...
        }
    
        /*
         Remove saved version if user reset(logout) the app.
         */
        public func reset() {
            self.storedVersion = nil
        }
    }
    
    
    //trigger version config with current version
    VersionConfig.shared.validate(currentVersion: "1.0.0")
    
    //reset when user logsout
    VersionConfig.shared.reset()
    

提交回复
热议问题