jinSasaki / InAppPurchase

A Simple and Lightweight framework for In App Purchase (Store Kit 1)
MIT License
288 stars 30 forks source link

Purchase method's completion handler is not called #39

Closed ArtSabintsev closed 5 years ago

ArtSabintsev commented 5 years ago

Describe the bug Neither the success nor the failure are called. I've completed a successful purchase many times, but I can't do anything after the purchase has completed.

To Reproduce My sample code.

    static func removeAds() {
        let iap = InAppPurchase.default
        iap.addTransactionObserver()
        iap.purchase(productIdentifier: "1") { result in
            switch result {
            case .success(let state):
                print("SUCCESS: \(state)")
                UserDefaults.standard.removeAds = true
            case .failure(let error):
                print(error)
            }
        }
        iap.removeTransactionObserver()
    }

Is something wrong my code?

Screenshots IMG_3627 UNADJUSTEDNONRAW_thumb_55df

Desktop (please complete the following information):

Smartphone (please complete the following information):

jinSasaki commented 5 years ago

The In-App-Purchase transaction should be always observed after your app launched. If you have no special reason, I recommend to call addTransactionObserver in didFinishLaunchingWithOptions of AppDelegate, and not to remove the observer during your app running.

Thanks for the sample code to help me understand your issue!

ArtSabintsev commented 5 years ago

No problem! Thank you, I’ll try in the morning!

Do I need to retain IAP.default in the AppDelegate, or does the Singleton self retain after calling it once?

Sent with GitHawk

jinSasaki commented 5 years ago

The latter is right. InAppPurchase.default is a shared singleton instance.

ArtSabintsev commented 5 years ago

ok, that fixed it - thank you!