In App Purchase Crashes on [[SKPaymentQueue defaultQueue] addPayment:payment]

后端 未结 2 907
眼角桃花
眼角桃花 2020-12-25 11:11

My In-App-Purchases work. I present a ModalView with a \"Buy\" UIButton. You click the button and the In App Purchase goes through the process. You can even do it several ti

相关标签:
2条回答
  • 2020-12-25 11:48

    I think observers added using addTransactionObserver are apparently weak references - not strong ones, which would explain this. I've made a simple test:

    // bad code below:
    // the reference is weak so the observer is immediately destroyed
    addTransactionObserver([[MyObserver alloc] init]);
    ...
    [[SKPaymentQueue defaultQueue] addPayment:payment]; // crash
    

    And got the same crash even without calling removeTransactionObserver. The solution in my case was to simply keep a strong reference to the observer:

    @property (strong) MyObserver* observer;
    ....
    self.observer = [[MyObserver alloc] init];
    addTransactionObserver(observer);
    
    0 讨论(0)
  • 2020-12-25 11:53

    The error message indicates a message is being sent to a deallocated instance of InAppPurchaseManager, which is your class. And it's happening after you open the view (creating an instance), close the view (releasing an instance), then opening the view again (creating a second instance). And the problem is happening within the addPayment: call. This indicates that the framework still has a handle on your old, released instance, and is trying to send it a message.

    You give the framework a handle to your object in loadStore, when you call

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    

    I don't see anywhere where you remove self as an observer. Objects that send out notifications usually do not retain their observers, since doing so can create a retain cycle and/or a memory leak.

    In your dealloc code you need to cleanup and call removeTransactionObserver:. That should solve your problem.

    0 讨论(0)
提交回复
热议问题