Check if my IOS application is updated

前端 未结 8 1073
爱一瞬间的悲伤
爱一瞬间的悲伤 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()
    
    0 讨论(0)
  • 2020-12-05 18:54

    You could save a value (e.g. the current app version number) to NSUserDefaults and check it every time the user starts the app.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // ...
    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
        NSString *previousVersion = [defaults objectForKey:@"appVersion"];
        if (!previousVersion) {
            // first launch
    
            // ...
    
            [defaults setObject:currentAppVersion forKey:@"appVersion"];
            [defaults synchronize];
        } else if ([previousVersion isEqualToString:currentAppVersion]) {
            // same version
        } else {
            // other version
    
            // ...
    
            [defaults setObject:currentAppVersion forKey:@"appVersion"];
            [defaults synchronize];
        }
    
    
    
        return YES;
    }
    

    The swift-2 version looks like this:

    let defaults = NSUserDefaults.standardUserDefaults()
    
    let currentAppVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
    let previousVersion = defaults.stringForKey("appVersion")
    if previousVersion == nil {
        // first launch
        defaults.setObject(currentAppVersion, forKey: "appVersion")
        defaults.synchronize()
    } else if previousVersion == currentAppVersion {
        // same version
    } else {
        // other version
        defaults.setObject(currentAppVersion, forKey: "appVersion")
        defaults.synchronize()
    }
    

    The swift-3 version looks like this:

    let defaults = UserDefaults.standard
    
    let currentAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
    let previousVersion = defaults.string(forKey: "appVersion")
    if previousVersion == nil {
        // first launch
        defaults.set(currentAppVersion, forKey: "appVersion")
        defaults.synchronize()
    } else if previousVersion == currentAppVersion {
        // same version
    } else {
        // other version
        defaults.set(currentAppVersion, forKey: "appVersion")
        defaults.synchronize()
    }
    
    0 讨论(0)
提交回复
热议问题