import ProgressHUD
class UpdateManager:NSObject{
/// app版本更新检测
///
/// - Parameter appId: apple ID - 开发者帐号对应app处获取
override init() {
super.init()
let appId:String = "1421026171"
//获取appstore上的最新版本号
let appUrl = URL.init(string: "http://itunes.apple.com/lookup?id=" + appId)
guard let appMsg = try? String.init(contentsOf: appUrl!, encoding: .utf8) else{
return
}
let appMsgDict:NSDictionary = getDictFromString(jString: appMsg)
let appResultsArray:NSArray = (appMsgDict["results"] as? NSArray)!
let appResultsDict:NSDictionary = appResultsArray.lastObject as! NSDictionary
let appStoreVersion:String = appResultsDict["version"] as! String
let appStoreVersion_Float:Float = Float(appStoreVersion)!
//获取当前手机安装使用的版本号
let localVersion:String = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
let localVersion_Float:Float = Float(localVersion)!
//用户是否设置不再提示
let userDefaults = UserDefaults.standard
let res = userDefaults.bool(forKey: "NO_ALERt_AGAIN")
//appstore上的版本号大于本地版本号 - 说明有更新
if appStoreVersion_Float > localVersion_Float && !res {
let alertC = UIAlertController.init(title: "版本更新了", message: "是否前往更新", preferredStyle: .alert)
let yesAction = UIAlertAction.init(title: "去更新", style: .default, handler: { (handler) in
self.updateApp(appId:appId)
})
let noAction = UIAlertAction.init(title: "下次再说", style: .cancel, handler: nil)
let cancelAction = UIAlertAction.init(title: "不再提示", style: .default, handler: { (handler) in
self.noAlertAgain()
})
alertC.addAction(yesAction)
alertC.addAction(noAction)
alertC.addAction(cancelAction)
UIApplication.shared.keyWindow?.rootViewController?.present(alertC, animated: true, completion: nil)
}else{
ProgressHUD.showSuccess("已经是最新版本了哦")
}
}
//去更新
func updateApp(appId:String) {
let updateUrl:URL = URL.init(string: "http://itunes.apple.com/app/id" + appId)!
UIApplication.shared.openURL(updateUrl)
// UIApplication.shared.open(updateUrl, options: [:], completionHandler: nil)
}
//不再提示
func noAlertAgain() {
let userDefaults = UserDefaults.standard
userDefaults.set(true, forKey: "NO_ALERt_AGAIN")
userDefaults.synchronize()
}
//JSONString转字典
func getDictFromString(jString:String) -> NSDictionary {
let jsonData:Data = jString.data(using: .utf8)!
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
if dict != nil {
return dict as! NSDictionary
}
return NSDictionary()
}
}
Swift App更新提示
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 当你的 app 版本更新之后,一般情况下用户是不会知道的,只有等到 App Store 的图片上有一个大大的"1"...