iOS開發之一句代碼檢測APP版本的更新-Swift版本


 
            
           

//新建一個NSObject類,將以下代碼拷貝到此類中。

 
          

class HKVersionManager: NSObject {

    

    init(appleId: String) {

        super.init()

        // 獲取appstore上的最新版本號

        let appUrl = URL.init(string: "http://itunes.apple.com/lookup?id=" + appleId)

        let appMsg = try? String.init(contentsOf: appUrl!, encoding: .utf8)

        let appMsgDict:NSDictionary = getDictFromString(jString: appMsg!)

        let appResultsArray:NSArray = (appMsgDict["results"] as? NSArray)!

        if appResultsArray.count==0 {

            return

        }

        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:appleId)

            })

            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)

        }

    }

    

    /// 去更新

    @available(iOS 10.0, *)

    func updateApp(appId:String) {

        let updateUrl:URL = URL.init(string: "http://itunes.apple.com/app/id" + appId)!

        if #available(iOS 10.0, *) {

            UIApplication.shared.open(updateUrl, options: [:], completionHandler: nil)

        } else {

            // Fallback on earlier versions

        }

    }

    

    /// 不再提示

    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")

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM