我有这个代码验证IAP收据,我试图根据返回此函数的状态显示警告,但我一直收到此错误
"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)"
这是我使用的代码,(非常简化)。我猜它与线程和异步的东西有关,我真的不太确定。我该怎么做正确的方法?
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)
}
}
这就是我调用函数的方式:
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()
}
答案 0 :(得分:1)
只需修改showMessage()
例程即可跳回主线程:
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。
或者你可以把这一行改为verifyPaymentReceipt()
,这样你从那里做的任何东西都会回到主线程。两者都是可以接受的选择。