美文网首页
iOS检查更新

iOS检查更新

作者: lalala1112389 | 来源:发表于2017-10-22 19:19 被阅读0次
在AppDelegate的DidFinish方法中调用
  self.checkUpdatePlist()
func checkUpdatePlist() {
        HWUtil.shared.checkUpdates({ (appVersion, ipaVersion, ipaDesc, ipaDownloadPath) in
            let desc = "当前版本号:\(appVersion)\n  最新版本号:\(ipaVersion)\(ipaDesc)"
            self.showUpdateAlert("软件有新版本可更新",desc, ipaDownloadPath)
        }, nil, nil)
    }
func checkUpdates(_ haveUpdate: ((_ appVersion: String, _ ipaVersion: String, _ ipaDescription: String, _ downloadPath: String)->())?, _ noUpdate: (() -> ())?, _ fail: ((URL?, URLResponse?, Error?) -> ())?) {
        DispatchQueue.global().async {
            // https://114.242.84.115/ota/ios/hwnews.plist
            let url = URL.init(string: "https://114.242.84.115/ota/ios/hwnews.plist")
            let request = URLRequest.init(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10)
            
            let session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
            
            let downlaodTask = session.downloadTask(with: request, completionHandler: { (url, response, error) in
                
                if error == nil {
                    
                    if let plistData = NSDictionary.init(contentsOf: url!) {
                        let ipaDownloadPath = "itms-services://?action=download-manifest&url=https://114.242.84.115/ota/ios/hwnews.plist"
                        
                        debugPrint(plistData)
                        let items = plistData["items"] as! [Any]
                        let itemDict = items.first as! [String: Any]
                        let metadata = itemDict["metadata"] as! [String:String]
                        let ipaVersion = metadata["bundle-version"]
                        let description = metadata["description"]
                        
                        let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
                        if (self.compareVersions(v1: ipaVersion!, v2: currentVersion!)) {
                            // 更新
                            haveUpdate?(currentVersion!, ipaVersion!, "", ipaDownloadPath) // 暂时隐藏
//                            haveUpdate?(currentVersion!, ipaVersion!, "\n\(description!)", ipaDownloadPath)
                        } else {
                            noUpdate?()
                        }
                        
                    } else {
                        noUpdate?()
                    }
                } else {
                    fail?(url, response, error)
                }
            })
            
            downlaodTask.resume()
        }
    }
//提示更新
    func showUpdateAlert(_ title: String, _ verisondescription: String, _ urlString: String) {
        let alert = UIAlertController.init(title: title, message: verisondescription, preferredStyle: .alert)
        let cancelAction = UIAlertAction.init(title: "以后再说", style: .cancel, handler: { [weak self] (action) in
            // cancel operation
            
            if UserDefaults.standard.object(forKey: kUserLogined) as! Bool == true {
                // 已登录
                 debugPrint("")
                
                let rootVC =  self?.window?.rootViewController
                
                if (rootVC?.isKind(of: LoginViewController.self))! {
                    // present
                    let tabBarController = BaseTabBarController()
                    tabBarController.delegate = self
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
                        rootVC?.present(tabBarController, animated: true, completion: nil)
                    })
                }
                
            }
        })
        let removeAction = UIAlertAction.init(title: "下载", style: .default, handler: { [weak self] (action) in
            
            if UserDefaults.standard.object(forKey: kUserLogined) as! Bool == true {
                // 已登录
                debugPrint("")
                 let rootVC =  self?.window?.rootViewController
                let tabBarController = BaseTabBarController()
                tabBarController.delegate = self
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
                    rootVC?.present(tabBarController, animated: true, completion: nil)
                })
            }
            
            // download operation
            print("打开 sarafi")
            let url = URL.init(string: urlString)
            
            if #available(iOS 10.0, *) {
                //高于 iOS 10.0
                UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                
            } else {
                //低于 iOS 10.0
                UIApplication.shared.openURL(url!)
            }
        })
        
        alert.addAction(cancelAction)
        alert.addAction(removeAction)
        
        self.window?.rootViewController?.present(alert, animated: false, completion: nil)
    }

相关文章

  • iOS检查更新

    //以get的形式提交,get的参数就是上面的域名,parameters的参数是一个字典类型,将上面的字典作为它的...

  • iOS检查更新

    自从iOS8系统开始,用户可以在设置里面设置在WiFi环境下,自动更新安装的App。此功能大大方便了用户,但是一些...

  • iOS检查更新

  • IOS 检查更新

    #pragma mark -检查更新 -(void)checkUpdateWithAPPID:(NSString*...

  • iOS 检查更新

    1、从苹果服务器上查询已发布的最新应用版本号 请求的URL地址: 返回的数据为json格式(包括开发者ID,开发者...

  • iOS开发之获取App Store的上架信息(包括版本号)并实现

    由于iOS app不允许检查更新,凡是app在上架审核时出现“检查更新”字样的都有可能被打回,而检查更新功能是产品...

  • IOS检查应用更新

    使用这个方法,只传入你的appId即可,在只有需要更新时才做处理。 不依赖三方,不依赖后台 ps: 1.只要在it...

  • iOS 检查App版本更新

    可以通过 iTunes 接口检查 App 版本更新,接口为: 请求返回数很多,其中重要信息如下: 然后通过当前Ap...

  • iOS 版本检查更新(iVersion)

    iVersion的使用 在这里介绍一个版本检查更新三方库的使用,在github上已经1.9K星了,使用也是非常的简...

  • iOS APP检查版本更新

    iOS程序自动提示更新的实现方案大致分为两种:第一种,自己服务器提供一个接口,告知相关app的当前版本,是否需要更...

网友评论

      本文标题:iOS检查更新

      本文链接:https://www.haomeiwen.com/subject/trrnuxtx.html