Update application for latest version
Many times we need to update the application in app store (version update) but user did not get a proper information regarding the updation of application, when he/she opens the application.
so now we can give a functionality like when user open application and any updation in app store then user will get pop up message to update the application.
for this we need to follow some steps:
1: we need to find the app store application version and current application version ,
get application version:
1 |
let currentVersion = info["CFBundleShortVersionString"] as? String |
get application version in app store:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
guard let info = Bundle.main.infoDictionary, let identifier = info["CFBundleIdentifier"] as? String, let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { throw VersionError.invalidBundleInfo } let data = try Data(contentsOf: url) guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else { throw VersionError.invalidResponse } if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String { print("version in app store", version); } |
2: Now combine this two and write a common method to check like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func isUpdateAvailable() throws -> Bool { guard let info = Bundle.main.infoDictionary, let currentVersion = info["CFBundleShortVersionString"] as? String, let identifier = info["CFBundleIdentifier"] as? String, let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { throw VersionError.invalidBundleInfo } let data = try Data(contentsOf: url) guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else { throw VersionError.invalidResponse } if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String { print("version in app store", version,currentVersion); return version != currentVersion } throw VersionError.invalidResponse } |
3: Now on your first view controller write this code on viewdidload()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
DispatchQueue.global().async { do { let update = try self.globalObjectHome.isUpdateAvailable() print("update",update) DispatchQueue.main.async { if update{ self.popupUpdateDialogue(); } } } catch { print(error) } } |
4: if the user click on update then move to app store.
Note:
copy the url of your application in app store and write like that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
func popupUpdateDialogue(){ var versionInfo = "" do { versionInfo = try self.globalObjectHome.getAppStoreVersion() }catch { print(error) } let alertMessage = "A new version of Mobikul Magento2 Application is available,Please update to version "+versionInfo; let alert = UIAlertController(title: "New Version Available", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) let okBtn = UIAlertAction(title: "Update", style: .default, handler: {(_ action: UIAlertAction) -> Void in if let url = URL(string: "itms-apps://itunes.apple.com/us/app/magento2-mobikul-mobile-app/id1166583793"), UIApplication.shared.canOpenURL(url){ if #available(iOS 10.0, *) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(url) } } }) let noBtn = UIAlertAction(title:"Skip this Version" , style: .destructive, handler: {(_ action: UIAlertAction) -> Void in }) alert.addAction(okBtn) alert.addAction(noBtn) self.present(alert, animated: true, completion: nil) } |