Tumgik
#InAppPurchaseManager
ngoclt · 12 years
Text
InAppPurchaseManager Phonegap 2.1 plugin, ARC Project,
I've tried to use InAppPurchaseManager plugin (Phonegap plugin) to implement IAP into an iOS Project. But this plugin is very old now (Check here and here).
After googled, I found an update version for this plugin, made by usmart. This link is source code which is used for non-ARC Project (this is my problem, have to make this code work on ARC Project). 
First, I used Xcode Extract Refactoring Feature to convert that code to Objective-C ARC. Unfortunately it didn't work. There is some memory leak in new code (after converted):
ProductsRequestDelegate* delegate = [[ProductsRequestDelegate alloc] init]; delegate.command = self; delegate.successCallback = [arguments objectAtIndex:1]; delegate.failCallback = [arguments objectAtIndex:2]; productsRequest.delegate = delegate;
BatchProductsRequestDelegate* delegate = [[BatchProductsRequestDelegate alloc] init]; delegate.command = self; delegate.callback = [arguments objectAtIndex:0]; productsRequest.delegate = delegate; [productsRequest start];
The problem is delegates are typically held by weak or assign properties, then they are deallocated before called. So we need to hold onto a strong or retain reference to our ProductsRequestDelegate and BatchProductsRequestDelegate.
Hmmm, there are some "dirty trick" here :">.
Add 2 new properties into InAppPurchaseManager class:
@property (nonatomic, strong) ProductsRequestDelegate* productDelegate; @property (nonatomic, strong) BatchProductsRequestDelegate* batchDelegate;
@synthesize productDelegate=_productDelegate, batchDelegate=_batchDelegate;
And then replace 2 delegate objects (ProductsRequestDelegate and BatchProductsRequestDelegate objects) with self.productDelegate and self.batchDelegate.
self.productDelegate = [[ProductsRequestDelegate alloc] init]; self.productDelegate.command = self; self.productDelegate.successCallback = [arguments objectAtIndex:1]; self.productDelegate.failCallback = [arguments objectAtIndex:2]; productsRequest.delegate = self.productDelegate; [productsRequest start];
self.batchDelegate = [[BatchProductsRequestDelegate alloc] init]; self.batchDelegate.command = self; self.batchDelegate.callback = [arguments objectAtIndex:0]; productsRequest.delegate = self.batchDelegate; [productsRequest start];
That's this. It should work now.
0 notes