APP升级,获取AppStore版本号和本地版本号--Swift

前端之家收集整理的这篇文章主要介绍了APP升级,获取AppStore版本号和本地版本号--Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
private func checkUpdate() {
        let path = NSString(format: "http://itunes.apple.com/cn/lookup?id=%@",AppConfig.appStoreID) as String
        let url = NSURL(string: path)
        let request = NSMutableURLRequest(URL: url!,cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,timeoutInterval: 10.0)
        request.HTTPMethod = "POST"
        
        NSURLConnection.sendAsynchronousRequest(request,queue: NSOperationQueue()) { (response,data,error) in
            let receiveStatusDic = NSMutableDictionary()
            if data != nil {
                do {
                    let dic = try NSJSONSerialization.JSONObjectWithData(data!,options: NSJSONReadingOptions.MutableContainers)
                    if let resultCount = dic["resultCount"] as? NSNumber {
                        if resultCount.integerValue > 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")
                                        NSUserDefaults.standardUserDefaults().setObject(version,forKey: "Version")
                                        NSUserDefaults.standardUserDefaults().synchronize()
                                    }
                                }
                            }
                        }
                    }
                }catch let error {
                    log.debug("checkUpdate -------- \(error)")
                    receiveStatusDic.setValue("0",forKey: "status")
                }
            }else {
                receiveStatusDic.setValue("0",forKey: "status")
            }
            self.performSelectorOnMainThread(#selector(AppDelegateSplash.checkUpdateWithData(_:)),withObject: receiveStatusDic,waitUntilDone: false)
        }
    }
    
    @objc private func checkUpdateWithData(data: NSDictionary) {
        let status = data["status"] as? String
        let localVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
        if status == "1" {
            let storeVersion = data["version"] as! String
            self.compareVersion(localVersion,storeVersion: storeVersion)
            return
        }
        if let storeVersion = NSUserDefaults.standardUserDefaults().objectForKey("Version") as? String {
            self.compareVersion(localVersion,storeVersion: storeVersion)
        }
    }
    
    private func compareVersion(localVersion: String,storeVersion: String) {
        if localVersion.compare(storeVersion) == NSComparisonResult.OrderedAscending {
            //做你想做的事情
        }
    }


注意,从APPStore获取信息不是每次都能获取成功的,所以需要在本地做个缓存,只要有一次成功就记住版本号

原文链接:https://www.f2er.com/swift/323187.html

猜你在找的Swift相关文章