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
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.
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()
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()
}