O2VersionManager.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // O2 App 版本管理
  3. // O2VersionManager.swift
  4. // O2Platform
  5. //
  6. // Created by FancyLou on 2019/11/15.
  7. // Copyright © 2019 zoneland. All rights reserved.
  8. //
  9. import UIKit
  10. import Alamofire
  11. import CocoaLumberjack
  12. import ObjectMapper
  13. import Promises
  14. class O2VersionManager {
  15. static let shared: O2VersionManager = {
  16. return O2VersionManager()
  17. }()
  18. private init() {}
  19. //检查版本
  20. func checkAppUpdate(call:@escaping (_ versionInfo: O2VersionInfoModel?, _ error :String?) -> Void) {
  21. call(nil, "") // 上架版本不需要应用内更新
  22. // if let infoPath = Bundle.main.path(forResource: "Info", ofType: "plist"), let dic = NSDictionary(contentsOfFile: infoPath) {
  23. // if let versionUrl = dic["o2 app version url"] as? String {
  24. // all(self.loadLastAppVersionInfo(url: versionUrl), self.appBuild()).then { (results) in
  25. // let ios = results.0
  26. // let appBuild = results.1
  27. // let onlineIosBuild = Int(string: ios.buildNo ?? "0") ?? 0
  28. // let thisAppBuild = Int(string: appBuild) ?? 0
  29. // if onlineIosBuild > thisAppBuild {
  30. // call(ios, nil)
  31. // }else {
  32. // call(nil, "没有新版本,不需要更新!")
  33. // }
  34. // }.catch { (err) in
  35. // DDLogError(err.localizedDescription)
  36. // call(nil, err.localizedDescription)
  37. // }
  38. // }else {
  39. // DDLogError("没有配置版本更新地址,无法检查更新!")
  40. // call(nil, "没有配置版本更新地址,无法检查更新!")
  41. // }
  42. // }else {
  43. // DDLogError("没有Info.plist文件。。。。。是吗???")
  44. // call(nil, "没有配置版本更新地址,无法检查更新!")
  45. // }
  46. }
  47. func updateAppVersion(_ downloadUrl: String?) {
  48. if let obj = downloadUrl {
  49. DDLogDebug("更新app地址:\(obj)")
  50. if let appURL = URL(string: obj) {
  51. if UIApplication.shared.canOpenURL(appURL) {
  52. UIApplication.shared.openURL(appURL)
  53. }else {
  54. DDLogError("地址不对,无法升级!")
  55. }
  56. }
  57. }else {
  58. DDLogError("download地址为空!")
  59. }
  60. }
  61. private func loadLastAppVersionInfo(url: String) -> Promise<O2VersionInfoModel> {
  62. return Promise { fulfill, reject in
  63. //获取版本信息
  64. if let timeUrl = AppDelegate.o2Collect.generateTimestampWithURL(url) {
  65. DDLogDebug("url with time : \(timeUrl)")
  66. AF.request(timeUrl).responseJSON {
  67. response in
  68. switch response.result {
  69. case .success(let val):
  70. let ios = JSON(val)["ios"]
  71. if let iosModel = Mapper<O2VersionInfoModel>().map(JSONString:ios.description) {
  72. let build = iosModel.buildNo
  73. let version = iosModel.versionName
  74. let url = iosModel.downloadUrl
  75. DDLogDebug("ios version:\(version ?? "") , build:\(build ?? ""), url:\(url ?? "")")
  76. fulfill(iosModel)
  77. }else {
  78. DDLogError("解析版本信息对象异常")
  79. reject(OOAppError.jsonMapping(message: "解析版本信息对象异常", statusCode: 1024, data: nil))
  80. }
  81. case .failure(let err):
  82. DDLogError(err.localizedDescription)
  83. reject(err)
  84. }
  85. }
  86. }
  87. }
  88. }
  89. private func appBuild() -> Promise<String> {
  90. return Promise { fulfill, reject in
  91. if let info = Bundle.main.infoDictionary {
  92. let buildId = info["CFBundleVersion"] as? String
  93. let version = info["CFBundleShortVersionString"] as? String
  94. DDLogDebug("build: \(buildId ?? "") , version:\(version ?? "")")
  95. fulfill(buildId ?? "")
  96. }else {
  97. DDLogError("没有系统 info !!!!!!")
  98. reject(OOAppError.common(type: "system", message: "没有系统 info !", statusCode: 1024))
  99. }
  100. }
  101. }
  102. }