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