Created
February 18, 2019 06:36
-
-
Save CenoX/a1905a249f9349bb8546f33c9ca80dc2 to your computer and use it in GitHub Desktop.
Simple version manager(?) for iOS App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
/// Struct that contains current version and all past versions. | |
struct AppVersion: Codable { | |
var currentVersion: String | |
var pastVersions: [String] | |
} | |
class VersionManager { | |
private enum UserDefaultsKeys: String { | |
case versions | |
} | |
public static let shared = VersionManager() | |
public static func initialize() { print(VersionManager.shared.versions.currentVersion) } | |
/// Struct that contains current version and all past versions. | |
public private(set) var versions: AppVersion | |
private var isNewVersion: Bool = false | |
private init() { | |
/// CFBundleShortVersionString | |
let bundleVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String | |
if let versionData = UserDefaults.standard.data(forKey: UserDefaultsKeys.versions.rawValue) { | |
do { | |
var decodedData = try JSONDecoder().decode(AppVersion.self, from: versionData) | |
if bundleVersion == decodedData.currentVersion { | |
versions = decodedData | |
debugPrint("Same version") | |
return | |
} | |
let savedVersion = decodedData.currentVersion | |
if VersionManager.compareVersion(from: bundleVersion, to: savedVersion) { | |
decodedData.currentVersion = bundleVersion | |
decodedData.pastVersions.append(savedVersion) | |
let encodedData = try JSONEncoder().encode(decodedData) | |
UserDefaults.standard.set(encodedData, forKey: UserDefaultsKeys.versions.rawValue) | |
UserDefaults.standard.synchronize() | |
isNewVersion = true | |
print("Current and past version updated.") | |
} | |
versions = decodedData | |
} catch let error { | |
print(error) | |
fatalError("Error while loading data from userdefaults.") | |
} | |
} else { | |
// New Data needed. | |
let newData = AppVersion(currentVersion: bundleVersion, pastVersions: []) | |
do { | |
let encodedData = try JSONEncoder().encode(newData) | |
UserDefaults.standard.set(encodedData, forKey: UserDefaultsKeys.versions.rawValue) | |
UserDefaults.standard.synchronize() | |
} catch let error { | |
print("Error while saving data into userdefaults.") | |
print(error) | |
} | |
versions = newData | |
isNewVersion = true | |
} | |
} | |
static private func compareVersion(from currentVersion: String, to pastVersion: String?) -> Bool { | |
guard let pastVersion = pastVersion else { return true /* No past version */ } | |
return currentVersion.compare(pastVersion, options: .numeric) == .orderedDescending | |
} | |
/// If app is new version, handler will excute. | |
public func isNewVersion(_ handler: () -> ()) { | |
if isNewVersion { | |
handler() | |
} | |
isNewVersion = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
VersionManager.shared.isNewVersion { | |
print("New version!!!") | |
// Showing Onboaring View... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment