Adding Observer to AppDelegate

Technical Note TN2387 "In-App Purchase Best Practices" is very helpful, and Listing 2 shows how to add a transaction observer, but...


It does not show example code for:


1. How to create a new observer in AppDelegate.

2. How to register with this observer in the class which calls

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{


Can someone please provide example Objective-C code or a link for 1 & 2 above? Using the "In App Programming Guide" and "Best Practices," has been very helpful. I just can't seem to get the app-wide observer working correctly.


Thanks in advance

Steve

Apple best practice requires that you always have in memory a class that is prepared to receive transactions. IMHO that is not best practice. I use a button that the user taps to instantiate a class that contains the observer and all the storekit stuff.


That said...


You asked how to add an observer to the App Delegate:

#import <StoreKit/StoreKit.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];


and add all of the StoreKit stuff in the App Delegate. (Bad idea)


Then you realized that the transaction observer isn't supposed to be in the app delegate. So what you do (again 'Apple best' but not 'best') is create a class that has all of that StoreKit stuff in it and load and initialize that class all in didFinishLaunchingWithOptions. In the init method of that class add the transaction observer to the class.


And, as alluded to above as 'better' than best: anywhere in your code have a button labeled 'get IAP info' and when that button is tapped, instantiate the StoreKit class with transaction observer. Lastly, if you are using autorenewables, do that button-tapping thing in the background when you detect an expired subscription.

PBK,

Thank you for your reply and for your suggestion about how to do "better than best!"

I am concerned that my app will crash if I create the transaction observer in a class, and then the user exists that class (its ViewController) while a transaction has not finished.

Perhaps I do not understand the persistence of observers?


Again, I am looking for the code on how to create a "shared", app-wide observer in AppDelegate. Your example shows addTransactionObserver:self, which we agree I do not want.


Thanks,

Steve

You are correct to be concerned that leaving a transactionObserver pointing to a terminated class could throw an exception.


In the class that you create to handle the transactions you will add:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];


You will have a removeTransactionObserver in the dealloc method of that class or in some other method that results in terminating that class. Alternatively you can create the class from the app delegate's didFinishLaunchingWithOptions method, add the transaction observer either from within the created class (using :self) or from the app delegate itself (using :myClassToHandleStoreTransactions)

I have the same issue and no answer so far. In my app I have created a different class TransactionObserver: NSObject, SKPaymentTransactionObserver


adding all the functions it requested and then in my AppDelegate I have an instanc:


var observe = TransactionObserver()


Adding later:


SKPaymentQueue.defaultQueue().addTransactionObserver(Observer)


I don't know if it is correct though.... let me know if you get to know this

Adding Observer to AppDelegate
 
 
Q