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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import SwiftyJSON import UIKit import Alamofire
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate, WXApiLogDelegate { var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { checkVersion()
window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = ViewController() window?.makeKeyAndVisible() return true }
func checkVersion() { AF.request("https://itunes.apple.com/lookup?bundleId=App的bundleId", method: .get, parameters: [:]) .responseJSON { response in print(response) if let value = response.value { let json = JSON(value) self.compareVersion(appStoreVersion: json["results"][0]["version"].stringValue) } } }
func compareVersion(appStoreVersion: String) { print(appStoreVersion) let version: String = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String)!
if appStoreVersion == version { print("当前已是最新版") } else { let alertC = UIAlertController(title: "有新版本", message: "前去更新版本?", preferredStyle: .alert) alertC.addAction(UIAlertAction(title: "去更新", style: .default, handler: { _ in self.updateApp(appId: AppID) })) alertC.addAction(UIAlertAction(title: "下次再说", style: .cancel, handler: { _ in })) UIApplication.shared.keyWindow?.rootViewController?.present(alertC, animated: true, completion: nil) } }
func updateApp(appId _: String) { let urlString = "itms-apps://itunes.apple.com/app/id\(AppID)" if let url = URL(string: urlString) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { _ in }) } else { UIApplication.shared.openURL(url) } } } }
|