每个版本都需要用到的当前app的版本
let infoDictionary : [String : Any] = Bundle.main.infoDictionary!
let currentVersion : String = infoDictionary["CFBundleShortVersionString"] as! String // 主程序版本号
// 第三版比对版本号更新,通过下面的网络请求获取AppStore的app信息,对比当前手机上的app版本号
// orderedAscending(-1):左操作数小于右操作数。
// orderedSame(0): 两个操作数相等。
// orderedDescending(1):左操作数大于右操作数。
// 这里利用的是App Store版本号对比当前手机app版本号,当数字是1时代表有新版本
NetworkNormalTools.getAppVersionRequest(url: "https://itunes.apple.com/lookup?id=appId", params: [:]) { (result) in
print(result)
let model = Mapper<AppStoreVersionResultModel>().map(JSON: (result as! [String : Any]))
let versionSize = model?.results.last?.version.compare(currentVersion, options: .caseInsensitive, range: nil, locale: nil).rawValue
if versionSize == 1 {
RemindTools.alertView(controller: self, message: "XXX有新版本了,赶快去更新呦!", ok: "好的", normalBackBlock: {
UIApplication.shared.openURL(URL(string: kAppStoreAddress)!)
})
}
}
第一版和第二版的版本号对比更新,需要后台进行配合,在app上架成功后,更新服务器的app版本号。第三版的比对更简单
// 第二版比对app版本号进行更新
let params : [String : Any] = ["type":"0"]
NetworkTools<VersionModel>.getRequest(url: GXB_GetVersion_Url, params: params) { (result) in
print(result)
let versionSize = result.version.compare(currentVersion, options: .caseInsensitive, range: nil, locale: nil).rawValue
if versionSize == 1 {
RemindTools.alertView(controller: self, message: "XXX有新版本了,赶快去更新呦!", ok: "好的", normalBackBlock: {
UIApplication.shared.openURL(URL(string: kAppStoreAddress)!)
})
}
// 第一版比对app版本号进行更新
let versionArray : [String] = result.version.components(separatedBy: ".")
let currentVersionArray : [String] = currentVersion.components(separatedBy: ".")
if Int(versionArray[0])! > Int(currentVersionArray[0])! {
} else if Int(versionArray[1])! > Int(currentVersionArray[1])! {
RemindTools.alertView(controller: self, message: "XXX有新版本了,赶快去更新呦!", ok: "好的", normalBackBlock: {
UIApplication.shared.openURL(URL(string: kAppStoreAddress)!)
})
} else if Int(versionArray[2])! > Int(currentVersionArray[2])! {
RemindTools.alertView(controller: self, message: "XXX有新版本了,赶快去更新呦!", ok: "好的", normalBackBlock: {
UIApplication.shared.openURL(URL(string: kAppStoreAddress)!)
})
}
if result.version != currentVersion {
RemindTools.alertView(controller: self, message: "XXX有新版本了,赶快去更新呦!", ok: "好的", normalBackBlock: {
UIApplication.shared.openURL(URL(string: kAppStoreAddress)!)
})
}
}