Implement class AppUpdater for checking AppVerison in Appstore
* call Singleton AppUpdater if needed * PopUp with UpdateInfo if AppStoreVersion is different to local
This commit is contained in:
parent
0b2b552d16
commit
714c17ee30
@ -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 = "<group>"; };
|
||||
C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = "<group>"; };
|
||||
C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
|
||||
C078408A24936C96006E4269 /* AppUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUpdater.swift; sourceTree = "<group>"; };
|
||||
C099579B246C5E5C0016AA22 /* DataService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataService.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
@ -185,6 +187,7 @@
|
||||
C04783EF24685995004961FB /* SettingsScene.swift */,
|
||||
3EAD889424801B6A0048A10A /* RoundTimer.swift */,
|
||||
AB671B242494ECF0003FBE8D /* EloHelper.swift */,
|
||||
C078408A24936C96006E4269 /* AppUpdater.swift */,
|
||||
);
|
||||
path = GoldWars;
|
||||
sourceTree = "<group>";
|
||||
@ -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 */,
|
||||
|
124
GoldWars/GoldWars/AppUpdater.swift
Normal file
124
GoldWars/GoldWars/AppUpdater.swift
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user