1. 程式人生 > >Swift【App版本更新】

Swift【App版本更新】

Swift有對應的版本更新庫(Siren),有需要的可以參考和使用。
iOS開發中,有時會有這種需求,在AppStore上出現新版本時,應用內彈窗提示使用者更新.自動提示更新的實現方案分為兩種: 第一種,自己伺服器提供一個介面,通過請求,獲取app的相關的版本資訊,如:是否需要更新,以及更新的地址等資訊 第二種,就是利用蘋果的appstore 提供的相關api進行查詢更新.http://itunes.apple.com/cn/lookup?id=你的APPId 採用方案1,實現邏輯: 1: 向自己伺服器請求當前版本資訊 2: 和App當前版本進行比較,如果返回的版本比當前本地版本新,彈窗並顯示更新日誌,根據點選的按鈕,控制使用者跳轉到AppStore更新
簡單實現 效果圖:
具體程式碼實現 [javascript] view plain copy
  1. struct VersionInfo {  
  2.     var url: String        //下載應用URL
  3.     var title: String       //title
  4.     var message: String       //提示內容
  5.     var must_update: Bool  //是否強制更新
  6.     var version: String     //版本
  7. }  
  8. class VersionManager: NSObject {  
  9.     //本地版本
  10.    privatestatic func localVersion() -> String? {  
  11.         return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String  
  12.     }  
  13.     static func versionUpdate() {  
  14.         //1 請求服務端資料,並進行解析,得到需要的資料
  15.         //2 版本更新
  16.         handleUpdate(VersionInfo(url: "應用下載地址", title: "有新版本啦!", message: 
    "提示更新內容,解決了xxx等一系列問題,新增了xxx等功能!", must_update: false, version: "3.5"))  
  17.     }  
  18.     /// 版本更新
  19.    privatestatic func handleUpdate(_ info: VersionInfo) {  
  20.         guard let localVersion = localVersion()else { return }  
  21.         if isIgnoreCurrentVersionUpdate(info.version) { return }  
  22.         if versionCompare(localVersion: localVersion, serverVersion: info.version) {  
  23.             let alert = UIAlertController(title: info.title, message: info.message, preferredStyle: .alert)  
  24.             let update = UIAlertAction(title: "立即更新", style: .default, handler: { action in
  25.                 UIApplication.shared.open(URL(string: info.url)!)  
  26.             })  
  27.             alert.addAction(update)  
  28.             if !info.must_update { //是否強制更新
  29.                 let cancel = UIAlertAction(title: "忽略此版本", style: .cancel, handler: { action in
  30.                     UserDefaults.standard.set(info.version, forKey: "IgnoreCurrentVersionUpdate")  
  31.                 })  
  32.                 alert.addAction(cancel)  
  33.             }  
  34.             if let vc = UIApplication.shared.keyWindow?.rootViewController {  
  35.                 vc.present(alert, animated: true, completion: nil)  
  36.             }  
  37.         }  
  38.     }  
  39.    // 版本比較
  40.    privatestatic func versionCompare(localVersion: String, serverVersion: String) -> Bool {  
  41.         let result = localVersion.compare(serverVersion, options: .numeric, range: nil, locale: nil)  
  42.         if result == .orderedDescending || result == .orderedSame{  
  43.             returnfalse
  44.         }  
  45.             returntrue
  46.     }  
  47.     // 是否忽略當前版本更新
  48.     privatestatic func isIgnoreCurrentVersionUpdate(_ version: String) -> Bool {  
  49.         return UserDefaults.standard.string(forKey: "IgnoreCurrentVersionUpdate") == version  
  50.     }  
  51. }  
簡單說明: 1 程式碼的實現很簡單,上面只是簡單寫了一個測試資料,真正的資料需要自己在每次程式啟動之後向服務端請求資料。 2 提供了獲取本地版本、版本更新、版本比較、是否忽略當前版本更新等4個方法。isIgnoreCurrentVersionUpdate方法是表示當用戶選擇忽略版本之後下次啟動程式,對於當前版本不再進行更新提示 Swift有對應的版本更新庫(Siren),有需要的可以參考和使用。