企业包_版本检查更新_定时返回结果

来源:互联网 发布:淘宝网男士大头皮鞋 编辑:程序博客网 时间:2024/05/01 09:27

 技术难点:

1、在10s,访问服务器上的plist文件,NSDictionary(contentsOfURL)这个方法,请求时间长,且阻断当前的UI主线程,progressHUD加载不出来,所以考虑把这个方法放到另一个线程,这就要用到异步。

2、定时这个问题,performanselector这个方法已经禁用了,所以要用nstimer,

        3、GCD能实现,progressHUD检测后回到主线程,但没有cancel的方法,dispatch_relase已经禁用,所以考虑NSoperation 这个可以cancel,但是没办法回到主线程,经学习后,发现NSOperationQueue.mainQueue().addOperationWithBlock,这就是回到主线程的方法,完美解决

4、另外附上一个大拿的博文,NSoperation比GCD更提倡使用,谢谢。

        https://blog.cnbluebox.com/blog/2014/07/01/cocoashen-ru-xue-xi-nsoperationqueuehe-nsoperationyuan-li-he-shi-yong/

1、设访问plist文件的operation为全局变量。

      var oper:NSBlockOperation!

2、编写版本检查更新的方法和定时取消检查更新的方法

func updateApp(){

        self.showprogressHUD()

        

        let que =NSOperationQueue()

        NSTimer.scheduledTimerWithTimeInterval(1000, target:self, selector: "gettingNotification", userInfo:nil, repeats: false)

        

        self.oper =NSBlockOperation{() -> Voidin

            let dict =NSDictionary(contentsOfURL: NSURL(string: "\(UPDATEURL)/dfdf.plist")!)

            print("\(dict)")

            NSOperationQueue.mainQueue().addOperationWithBlock({ () ->Void in

                if (dict !=nil){

                    let list = dict?.objectForKey("items")as! Array<AnyObject>

                    let dict2 = list[0]

                    let dict3 = dict2.objectForKey("metadata")

                    let newVersion = dict3?.objectForKey("bundle-version")as! String

                    let myVersion =NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]as! String

                    self.removeLoadingView()

                    if(newVersion != myVersion){

                        let alertView =UIAlertView(title: "提示", message:"发现新版本", delegate:self, cancelButtonTitle: "确认")

                        alertView.tag =12001

                        alertView.show()

                    }else{

                        UIAlertView(title:"提示", message:"您已经是最新版", delegate:self, cancelButtonTitle: "确认").show()

                    }

                }else{

                    self.removeprogressHUD()

                }

                

            })

        }

        que.addOperation(self.oper)

    }


    func gettingNotification(){

        self.oper.cancel()

        self.removeprogressHUD()

    }

    

0 0