检查App Store版本,提示更新
//MARK: - 检查更新
fileprivate func checkUpdate() {
let path = NSString(format: "http://itunes.apple.com/cn/lookup?id=你的AppId") as String
let url = URL(string: path)
let request = NSMutableURLRequest(url: url!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
request.httpMethod = "POST"
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue()) { (response, data, error) in
let receiveStatusDic = NSMutableDictionary()
if data != nil {
do {
let dic:NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
if let resultCount = dic["resultCount"] as? NSNumber {
print("???:\(response) &&版本信息: \(dic)")
if resultCount.intValue > 0 {
receiveStatusDic.setValue("1", forKey: "status")
if let arr = dic["results"] as? NSArray {
if let dict = arr.firstObject as? NSDictionary {
if let version = dict["version"] as? String {
receiveStatusDic.setValue(version, forKey: "version")
UserDefaults.standard.set(version, forKey: "Version")
UserDefaults.standard.synchronize()
}
//更新内容
if let releaseNotes = dict["releaseNotes"] as? String {
UserDefaults.standard.set(releaseNotes, forKey: "releaseNotes")
UserDefaults.standard.synchronize()
}
}
}
}
}
}catch let error {
log.debug("checkUpdate -------- \(error)")
receiveStatusDic.setValue("0", forKey: "status")
}
}else {
receiveStatusDic.setValue("0", forKey: "status")
}
self.performSelector(onMainThread: #selector(SplashViewController.checkUpdateWithData(_:)), with: receiveStatusDic, waitUntilDone: false)
}
}
@objc fileprivate func checkUpdateWithData(_ data: NSDictionary) {
let status = data["status"] as? String
if status == "1" {
if let storeVersion = UserDefaults.standard.object(forKey: "Version") as? String {
if let releaseNotes = UserDefaults.standard.object(forKey: "releaseNotes") as? String {
self.compareVersion(LOCAL_VERSION, storeVersion: storeVersion,note:releaseNotes)
return
}
}
}
}
fileprivate func compareVersion(_ localVersion: String, storeVersion: String,note:String) {
let message = "本次更新内容:\n\(note)"
if localVersion.compare(storeVersion) == ComparisonResult.orderedAscending {
let alertView = UIAlertView(title: "发现新版本",message: message,delegate: self,cancelButtonTitle: nil,otherButtonTitles: "马上更新","下次再说")
alertView.delegate = self
alertView.tag = 10086
alertView.show()
}
}
override func alertView(_ alertView:UIAlertView, clickedButtonAt buttonIndex: Int){
if(alertView.tag == 10086) {
if(buttonIndex == 0){
UIApplication.shared.openURL(URL(string:"itms-apps://itunes.apple.com/cn/app/xing-rui-yu-le/id你的AppId?mt=8")!)
}else{
//下次再说
}
}
}
效果
获取本地版本
let LOCAL_VERSION:String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
弹出评价app的的提示
1.判断是否是新安装或刚升级应用(也可用于设置引导页时的判断)
//MARK: 判断是否是新安装或刚升级应用
func isNewVersion() -> Bool{
if(OLD_VERSION != nil){
let oldVersion = OLD_VERSION as! String
if(LOCAL_VERSION != oldVersion){//刚更新
NSUserDefaults.standardUserDefaults().setObject(LOCAL_VERSION, forKey:"oldVersion")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
}else{//新安装
NSUserDefaults.standardUserDefaults().setObject(LOCAL_VERSION, forKey:"oldVersion")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
let OLD_VERSION = NSUserDefaults.standardUserDefaults().valueForKey("oldVersion")
2.下面的代码可以放在弹出提示的视图控制器或父类里
//评价App
func judgeApp(){
let alert = UIAlertView(title: "评价XX",message: "预祝您观演愉快,您的评价可以帮我们做得更好哦!",delegate: self,cancelButtonTitle: "满足下你",otherButtonTitles: "别再烦我","下次再说")
alert.cancelButtonIndex = 0
alert.delegate = self
alert.show()
}
func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
if(buttonIndex == 0) {//去评价
UIApplication.sharedApplication().openURL(NSURL(string:"itms-apps://itunes.apple.com/cn/app/xing-rui-yu-le/id你的APPId?mt=8")!)
}
if(buttonIndex == 1) {//不再提示
NSUserDefaults.standardUserDefaults().setObject(true, forKey:"NeverShow")
NSUserDefaults.standardUserDefaults().synchronize()
}
if(buttonIndex == 2) {
//do nothing
}
}
3.调用的地方(放在你需要的地方弹出)
if let willJudge = NSUserDefaults.standardUserDefaults().valueForKey("NeverShow"){
if(willJudge as! Bool == true){//不再提示为 true ,但是新版本,弹出提示且关闭不再提示
let delegate:AppDelegate = SHARE_APPLICATION.delegate as! AppDelegate
if(delegate.isNewVersion() == true){//刚更新过
judgeApp()
NSUserDefaults.standardUserDefaults().setObject(false, forKey:"NeverShow")
NSUserDefaults.standardUserDefaults().synchronize()
}else{//不再提示值为 true ,且不是新版本,不弹出提示
//不弹出
}
}else{//不再提示值为 false ,弹出提示
judgeApp()
}
}else{ //不再提示值为 nil ,弹出提示
judgeApp()
}