与session.dataTaskWithRequest的UIAlertView问题 [英] UIAlertView issue with session.dataTaskWithRequest

查看:100
本文介绍了与session.dataTaskWithRequest的UIAlertView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码验证IAP收据,我试图根据返回此函数的状态显示警告但我一直收到此错误

I have this code that verifies IAP receipts and Im trying to show an alert based on what status this function is returned with but I keep getting this error

"This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release. ....... ( then a whole list of numbers and stuff)"

这是我使用的代码,(非常简化)。我猜测它与线程和异步的东西有关,我真的不太确定。我该怎么做?

This is the code Im using, (pretty simplified). Im guessing it has something to do with threads and asynchronus stuff that Im not really too sure about. How do I do this the right way?

 func verifyPaymentReceipt(transaction: SKPaymentTransaction, completion : (status : Bool) -> ()) {

 .... //Verify Receipt code

 let task = session.dataTaskWithRequest(storeRequest, completionHandler: { data, response, error  in

    if(error != nil){
      //Handle Error
    }
    else{
      completion(status: true)
    }

}

这就是我调用函数的方式:

And this is how I am calling the function :

verifyPaymentReceipt(transaction, completion: { (status) -> () in
                        if status {
                            print("Success")
                            self.showMessage(true)
                        } else {
                            print("Fail")
                            self.showMessage(false)
                        }
                    })

这是节目消息功能

    func showMessage(message: Bool) {

    var alert = UIAlertView()
    if message == true {
        alert = UIAlertView(title: "Thank You", message: "Your purchase(s) are succeessful", delegate: nil, cancelButtonTitle: "Ok")
    } else {
        alert = UIAlertView(title: "Thank You", message: "Your purchase(s) restore failed, Please try again.", delegate: nil, cancelButtonTitle: "Ok")
    }
    alert.show()

}


推荐答案

只需修改 showMessage()例程,跳回主线程:

Just modify your showMessage() routine to jump back onto the main thread:

func showMessage(message: Bool) {
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        var alert = UIAlertView()
        if message == true {
            alert = UIAlertView(title: "Thank You", message: "Your purchase(s) are succeessful", delegate: nil, cancelButtonTitle: "Ok")
        } else {
            alert = UIAlertView(title: "Thank You", message: "Your purchase(s) restore failed, Please try again.", delegate: nil, cancelButtonTitle: "Ok")
        }
        alert.show()
    })
}

虽然您可能还想更新以使用 UIAlertController 而不是 UIAlertView ,已被弃用。除非你仍然需要支持iOS 7。

Although you probably also want to update to use UIAlertController instead of UIAlertView, which has been deprecated. Unless you still need to support iOS 7.

或者你可以将此行放入 verifyPaymentReceipt()中,所以你从那里做的任何东西都会回到主线程。两者都是可以接受的选择。

Or you could put this line into verifyPaymentReceipt() instead, so that anything you do from there gets back to the main thread. Both are acceptable options.

这篇关于与session.dataTaskWithRequest的UIAlertView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆