码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发之一句代码检测APP版本的更新-Swift版本

时间:2017-12-30 12:39:00      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:appid   UI   super   之一   tab   for   检测   分享   cal   


技术分享图片

//新建一个NSObject类,将以下代码拷贝到此类中。

//
//  HKCheckVersionManager.swift
//  ProjectManager
//
//  Created by isHakan on 2017/12/29.
//  Copyright ? 2017年 liuhuakun. All rights reserved.
//

import UIKit

class HKCheckVersionManager: NSObject {
    
    /// app版本更新检测
    ///
    /// - Parameter appId: apple ID - 开发者帐号对应app处获取
    init(appId:String) {
        super.init()
       
        //获取appstore上的最新版本号
        let appUrl = URL.init(string: "http://itunes.apple.com/lookup?id=" + appId)
        let appMsg = try? String.init(contentsOf: appUrl!, encoding: .utf8)
        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)
        }
        
    }
    
    //去更新
    func updateApp(appId:String) {
        let updateUrl:URL = URL.init(string: "http://itunes.apple.com/app/id" + appId)!
        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()
    }
    
}

  在需要检测的类中调用一句代码即可

        //版本检测
        _ = HKCheckVersionManager.init(appId: "你的apple id")

  

iOS开发之一句代码检测APP版本的更新-Swift版本

标签:appid   UI   super   之一   tab   for   检测   分享   cal   

原文地址:https://www.cnblogs.com/liuhuakun/p/8146818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!