Reputation: 6472
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
Upvotes: 4
Views: 1014
Reputation: 845
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;
}
Upvotes: 1
Reputation: 6472
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).
Upvotes: 8