In-App Purchases: Crash when view disappear

孤街醉人 提交于 2019-12-13 05:26:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!