From 714c17ee302e9c2d422f890720265c3a862db186 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Fri, 12 Jun 2020 10:25:36 +0200 Subject: [PATCH] Implement class AppUpdater for checking AppVerison in Appstore * call Singleton AppUpdater if needed * PopUp with UpdateInfo if AppStoreVersion is different to local --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 + GoldWars/GoldWars/AppUpdater.swift | 124 ++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 GoldWars/GoldWars/AppUpdater.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 741ce81..9abc352 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -54,6 +54,7 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A7246C0EA50022B228 /* LabelNode.swift */; }; C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A9246C114C0022B228 /* LabelComponent.swift */; }; C064E9AC246C151F0022B228 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9AB246C151F0022B228 /* Label.swift */; }; + C078408B24936C96006E4269 /* AppUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = C078408A24936C96006E4269 /* AppUpdater.swift */; }; C099579C246C5E5C0016AA22 /* DataService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C099579B246C5E5C0016AA22 /* DataService.swift */; }; /* End PBXBuildFile section */ @@ -120,6 +121,7 @@ C064E9A7246C0EA50022B228 /* LabelNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = ""; }; C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = ""; }; C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = ""; }; + C078408A24936C96006E4269 /* AppUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUpdater.swift; sourceTree = ""; }; C099579B246C5E5C0016AA22 /* DataService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataService.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -185,6 +187,7 @@ C04783EF24685995004961FB /* SettingsScene.swift */, 3EAD889424801B6A0048A10A /* RoundTimer.swift */, AB671B242494ECF0003FBE8D /* EloHelper.swift */, + C078408A24936C96006E4269 /* AppUpdater.swift */, ); path = GoldWars; sourceTree = ""; @@ -421,6 +424,7 @@ 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, + C078408B24936C96006E4269 /* AppUpdater.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, ABC0C3732481509300387B8F /* MapUtils.swift in Sources */, diff --git a/GoldWars/GoldWars/AppUpdater.swift b/GoldWars/GoldWars/AppUpdater.swift new file mode 100644 index 0000000..5e2895b --- /dev/null +++ b/GoldWars/GoldWars/AppUpdater.swift @@ -0,0 +1,124 @@ +// +// AppUpdater.swift +// GoldWars +// +// Created by Tim Herbst on 12.06.20. +// Copyright © 2020 SP2. All rights reserved. +// +import UIKit + +enum VersionError: Error { + case invalidBundleInfo, invalidResponse +} + +class LookupResult: Decodable { + var results: [AppInfo] +} + +class AppInfo: Decodable { + var version: String + var trackViewUrl: String +} + +class AppUpdater: NSObject { + static let appUpdaterInstance = AppUpdater() + + private override init() { + + } + + private func getAppInfo(completion: @escaping (AppInfo?, Error?) -> Void) -> URLSessionDataTask? { + guard let identifier = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String, + let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { + DispatchQueue.main.async { + completion(nil, VersionError.invalidBundleInfo) + } + return nil + } + let task = URLSession.shared.dataTask(with: url) { (data, response, error) in + do { + if error != nil { throw error! } + guard let data = data else { throw VersionError.invalidResponse } + + print("Data:::",data) + print("response###",response!) + + let result = try JSONDecoder().decode(LookupResult.self, from: data) + + let dictionary = try? JSONSerialization.jsonObject(with: data, options: .mutableLeaves) + + print("dictionary",dictionary!) + + + guard let info = result.results.first else { throw VersionError.invalidResponse } + print("result:::",result) + completion(info, nil) + } catch { + completion(nil, error) + } + } + task.resume() + print("task ******", task) + return task + } + + private func checkVersion(force: Bool) { + let info = Bundle.main.infoDictionary + let currentVersion = info?["CFBundleShortVersionString"] as? String + _ = getAppInfo { (info, error) in + let appStoreVersion = info?.version + if let error = error { + dump(error) + } else if appStoreVersion!.compare(currentVersion!, options: .numeric) { + DispatchQueue.main.async { + //TODO: Really need UIKit-Alert? -> replace keyWindow (deprecated) + let topController: UIViewController = UIApplication.shared.windows[0].rootViewController! + + topController.showAppUpdateAlert(Version: (info?.version)!, Force: force, AppURL: (info?.trackViewUrl)!) + //END TODO + } + } + } + } + func showUpdateWithConfirmation() { + checkVersion(force : false) + } + func showUpdateWithForce() { + checkVersion(force : true) + } +} +extension UIViewController { + fileprivate func showAppUpdateAlert( Version : String, Force: Bool, AppURL: String) { + print("AppURL:::::",AppURL) + + let bundleName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String; + let alertMessage = "\(bundleName) Version \(Version) is available on AppStore." + let alertTitle = "New Version" + + + let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert) + + + if !Force { + let notNowButton = UIAlertAction(title: "Not Now", style: .default) { (action:UIAlertAction) in + print("Don't Call API"); + } + alertController.addAction(notNowButton) + } + + let updateButton = UIAlertAction(title: "Update", style: .default) { (action:UIAlertAction) in + print("Call API"); + print("No update") + guard let url = URL(string: AppURL) else { + return + } + if #available(iOS 13.2, *) { + UIApplication.shared.open(url, options: [:], completionHandler: nil) + } else { + UIApplication.shared.openURL(url) + } + } + alertController.addAction(updateButton) + self.present(alertController, animated: true, completion: nil) + } +}