Skip to content

Commit

Permalink
made it a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
sobri909 committed May 21, 2012
1 parent 77a3630 commit 391775b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions MGStoreKit.h
Expand Up @@ -14,6 +14,8 @@ typedef void (^PurchaseCallback)(NSString *productId);
@interface MGStoreKit : NSObject @interface MGStoreKit : NSObject
<SKProductsRequestDelegate, SKPaymentTransactionObserver> <SKProductsRequestDelegate, SKPaymentTransactionObserver>


+ (MGStoreKit *)store;

// fetch products data // fetch products data
- (void)requestProductsData:(NSSet *)products - (void)requestProductsData:(NSSet *)products
callback:(ProductsCallback)callback; callback:(ProductsCallback)callback;
Expand All @@ -25,6 +27,8 @@ typedef void (^PurchaseCallback)(NSString *productId);
- (void)purchaseProduct:(NSString *)productId - (void)purchaseProduct:(NSString *)productId
success:(PurchaseCallback)callback success:(PurchaseCallback)callback
failed:(PurchaseCallback)failed; failed:(PurchaseCallback)failed;

// restore previous purchases
- (void)restore:(ProductsCallback)callback; - (void)restore:(ProductsCallback)callback;


@property (nonatomic, retain) NSMutableDictionary *cachedProducts; @property (nonatomic, retain) NSMutableDictionary *cachedProducts;
Expand Down
14 changes: 13 additions & 1 deletion MGStoreKit.m
Expand Up @@ -19,16 +19,26 @@ @interface MGStoreKit ()


@implementation MGStoreKit @implementation MGStoreKit


static MGStoreKit *singleton;

@synthesize cachedProducts, productsRequest; @synthesize cachedProducts, productsRequest;
@synthesize productsCallback, purchaseCallback, failedCallback, restoreCallback; @synthesize productsCallback, purchaseCallback, failedCallback, restoreCallback;


+ (void)initialize {
singleton = [[MGStoreKit alloc] init];
}

- (id)init { - (id)init {
self = [super init]; self = [super init];
self.cachedProducts = [NSMutableDictionary dictionary]; self.cachedProducts = [NSMutableDictionary dictionary];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
return self; return self;
} }


+ (MGStoreKit *)store {
return singleton;
}

- (void)requestProductsData:(NSSet *)keys - (void)requestProductsData:(NSSet *)keys
callback:(ProductsCallback)callback { callback:(ProductsCallback)callback {
BOOL haveAllCached = YES; BOOL haveAllCached = YES;
Expand All @@ -49,7 +59,9 @@ - (void)requestProductsData:(NSSet *)keys
self.productsRequest = self.productsRequest =
[[SKProductsRequest alloc] initWithProductIdentifiers:keys]; [[SKProductsRequest alloc] initWithProductIdentifiers:keys];
self.productsRequest.delegate = self; self.productsRequest.delegate = self;
[self.productsRequest start]; dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.productsRequest start];
});
} }


- (BOOL)canMakePayments { - (BOOL)canMakePayments {
Expand Down

0 comments on commit 391775b

Please sign in to comment.