问题描述
我有验证 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 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!