问题
My application has a tabbar that contains 4 view controllers. The third view controller contains a "store in-app purchases". In this controller I use an object that manages in-app purchases (product request, purchase, transaction etc...) that allow me to get and show price description ecc.
The problem is: If I change tabs while the request started the app crashes sometimes, but not always.
I've to cancel the request in viewDidDisappear? [productsRequest cancel] this code crashes.
回答1:
I have the same issue. To fix it cancel request and all be fine.
var request: SKProductsRequest! //global to cancel when disappear
//request products when you want (viewDidLoad for example)
request = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
And when disapear viewcontroller:
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
request.delegate = nil;
request.cancel()
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
}
回答2:
Your problem has probably nothing to do with in-app purchase. Somewhere in your code you're sending a message to an object which has been released. Running the analyzer can help you find the bug, but it may not be necessary this time. If [productsRequest cancel] crashes, then probably productsRequest has too low retain count.
回答3:
Remove the TransactionObserver while your viewDidDisappear:
[[SKPaymentQueue defaultQueue]removeTransactionObserver:self];
If you go back from the Inapp viewcontroller to another viewcontroller,then
[[SKPaymentQueue defaultQueue]removeTransactionObserver:self];
[self dismissViewControllerAnimated:YES completion:NULL];
来源:https://stackoverflow.com/questions/7620901/in-app-purchases-crash-when-view-disappear