简体   繁体   中英

How to know that SKStoreKit In App Purchses caused applicationWillResignActive?

Is it normal when you submit an In-App Purchase, that it causes applicationWillResignActive while it asks you if you want to BUY?

For example:

    [[SKPaymentQueue defaultQueue] addPayment:payment];

This causes app to resign active and then once you hit BUY or CANCEL and then applicationWillEnterForeground is called.

Is there a way to know that it was an in app purchase that caused the application to resign so that when it enters foreground again, I can flag some things to be skipped?

Thanks

Ok, this is what I am going to do since I cannot think of any other way of doing it...

When an application starts up fresh it calls application:didFinishLaunchingWithOptions, and when it starts from the background it calls applicationWillEnterForeground. In both these two cases, it then always calls applicationDidBecomeActive, which is where I have the code that I want to skip when an IAP occurs.

When an application shuts down or moves to the background it always calls applicationWillResignActive and then applicationDidEnterBackground.

What I noticed is that an IAP calls applicationWillResignActive and then applicationDidBecomeActive and nothing else.

So in application:didFinishLaunchingWithOptions I will set a variable startupDidFinish=1

And in applicationWillEnterForeground I will set a variable startupForeground=1

In applicationDidBecomeActive I will do this:

    //SKIP if application resigned active then becomes active again.
    if (startupDidFinish == 1 || startupForeground==1) {
        //Do normal startup stuff

    }
    startupDidFinish = 0;
    startupForeground = 0;

So this will allow you to skip code for things like IAP (and I think also an SMS acts the same way).

I found a way to know if it was cause by in App purchase.

Call this method during the applicationWillResignActive method:

- (BOOL)checkIfTheUserIsDoingInAppPurchase {

for (SKPaymentTransaction* transaction in [[SKPaymentQueue defaultQueue] transactions]) {
    if(transaction.transactionState == SKPaymentTransactionStatePurchasing) {
        return YES;
    }
}
return NO;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM