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.       
  9. class VersionManager: NSObject {  
  10.       
  11.     //本地版本  
  12.    private static func localVersion() -> String? {  
  13.         return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String  
  14.     }  
  15.       
  16.     static func versionUpdate() {  
  17.         //1 请求服务端数据,并进行解析,得到需要的数据  
  18.         //2 版本更新  
  19.         handleUpdate(VersionInfo(url: "应用下载地址", title: "有新版本啦!", message: "提示更新内容,解决了xxx等一系列问题,新增了xxx等功能!", must_update: false, version: "3.5"))  
  20.     }  
  21.       
  22.     /// 版本更新  
  23.    private static func handleUpdate(_ info: VersionInfo) {  
  24.         guard let localVersion = localVersion()else { return }  
  25.         if isIgnoreCurrentVersionUpdate(info.version) { return }  
  26.         if versionCompare(localVersion: localVersion, serverVersion: info.version) {  
  27.             let alert = UIAlertController(title: info.title, message: info.message, preferredStyle: .alert)  
  28.             let update = UIAlertAction(title: "立即更新", style: .default, handler: { action in  
  29.                 UIApplication.shared.open(URL(string: info.url)!)  
  30.             })  
  31.             alert.addAction(update)  
  32.             if !info.must_update { //是否强制更新  
  33.                 let cancel = UIAlertAction(title: "忽略此版本", style: .cancel, handler: { action in  
  34.                     UserDefaults.standard.set(info.version, forKey: "IgnoreCurrentVersionUpdate")  
  35.                 })  
  36.                 alert.addAction(cancel)  
  37.             }  
  38.             if let vc = UIApplication.shared.keyWindow?.rootViewController {  
  39.                 vc.present(alert, animated: true, completion: nil)  
  40.             }  
  41.         }  
  42.     }  
  43.       
  44.    // 版本比较  
  45.    private static func versionCompare(localVersion: String, serverVersion: String) -> Bool {  
  46.         let result = localVersion.compare(serverVersion, options: .numeric, range: nil, locale: nil)  
  47.         if result == .orderedDescending || result == .orderedSame{  
  48.             return false  
  49.         }  
  50.             return true  
  51.     }  
  52.       
  53.     // 是否忽略当前版本更新  
  54.     private static func isIgnoreCurrentVersionUpdate(_ version: String) -> Bool {  
  55.         return UserDefaults.standard.string(forKey: "IgnoreCurrentVersionUpdate") == version  
  56.     }  
  57. }  

简单说明:
1 代码的实现很简单,上面只是简单写了一个测试数据,真正的数据需要自己在每次程序启动之后向服务端请求数据。
2 提供了获取本地版本、版本更新、版本比较、是否忽略当前版本更新等4个方法。isIgnoreCurrentVersionUpdate方法是表示当用户选择忽略版本之后下次启动程序,对于当前版本不再进行更新提示


Swift有对应的版本更新库(Siren),有需要的可以参考和使用。