forked from MKSG-MugunthKumar/MKStoreKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMKStoreManager.m
651 lines (532 loc) · 22.3 KB
/
MKStoreManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//
// MKStoreManager.m
// MKStoreKit (Version 4.0)
//
// Created by Mugunth Kumar on 17-Nov-2010.
// Version 4.1
// Copyright 2010 Steinlogic. All rights reserved.
// File created using Singleton XCode Template by Mugunth Kumar (http://mugunthkumar.com
// Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above
// Read my blog post at http://mk.sg/1m on how to use this code
// Licensing (Zlib)
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// As a side note on using this code, you might consider giving some credit to me by
// 1) linking my website from your app's website
// 2) or crediting me inside the app's credits page
// 3) or a tweet mentioning @mugunthkumar
// 4) A paypal donation to [email protected]
#import "MKStoreManager.h"
#import "SFHFKeychainUtils.h"
#import "MKSKSubscriptionProduct.h"
#import "MKSKProduct.h"
@interface MKStoreManager () //private methods and properties
@property (nonatomic, copy) void (^onTransactionCancelled)();
@property (nonatomic, copy) void (^onTransactionCompleted)(NSString *productId);
@property (nonatomic, copy) void (^onRestoreFailed)(NSError* error);
@property (nonatomic, copy) void (^onRestoreCompleted)();
@property (nonatomic, strong) NSMutableArray *purchasableObjects;
@property (nonatomic, strong) NSMutableDictionary *subscriptionProducts;
@property (nonatomic, strong) MKStoreObserver *storeObserver;
@property (nonatomic, assign, getter=isProductsAvailable) BOOL isProductsAvailable;
- (void) requestProductData;
- (void) startVerifyingSubscriptionReceipts;
-(void) rememberPurchaseOfProduct:(NSString*) productIdentifier;
-(void) addToQueue:(NSString*) productId;
@end
@implementation MKStoreManager
@synthesize purchasableObjects = _purchasableObjects;
@synthesize storeObserver = _storeObserver;
@synthesize subscriptionProducts;
@synthesize isProductsAvailable;
@synthesize onTransactionCancelled;
@synthesize onTransactionCompleted;
@synthesize onRestoreFailed;
@synthesize onRestoreCompleted;
static MKStoreManager* _sharedStoreManager;
+(void) updateFromiCloud:(NSNotification*) notificationObject {
NSLog(@"Updating from iCloud");
NSUbiquitousKeyValueStore *iCloudStore = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *dict = [iCloudStore dictionaryRepresentation];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSError *error = nil;
[SFHFKeychainUtils storeUsername:key
andPassword:obj
forServiceName:@"MKStoreKit"
updateExisting:YES
error:&error];
if(error)
NSLog(@"%@", [error localizedDescription]);
}];
}
+(BOOL) iCloudAvailable {
if(NSClassFromString(@"NSUbiquitousKeyValueStore")) { // is iOS 5?
if([NSUbiquitousKeyValueStore defaultStore]) { // is iCloud enabled
return YES;
}
}
return NO;
}
- (void)dealloc {
_purchasableObjects = nil;
_storeObserver = nil;
onTransactionCancelled = nil;
onTransactionCompleted = nil;
onRestoreFailed = nil;
onRestoreCompleted = nil;
}
+ (void) dealloc
{
_sharedStoreManager = nil;
[super dealloc];
}
+(void) setObject:(id) object forKey:(NSString*) key
{
NSString *objectString = nil;
if([object isKindOfClass:[NSData class]])
{
objectString = [[NSString alloc] initWithData:object encoding:NSUTF8StringEncoding];
}
if([object isKindOfClass:[NSNumber class]])
{
objectString = [(NSNumber*)object stringValue];
}
NSError *error = nil;
[SFHFKeychainUtils storeUsername:key
andPassword:objectString
forServiceName:@"MKStoreKit"
updateExisting:YES
error:&error];
if(error)
NSLog(@"%@", [error localizedDescription]);
if([self iCloudAvailable]) {
[[NSUbiquitousKeyValueStore defaultStore] setObject:objectString forKey:key];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
}
+(id) objectForKey:(NSString*) key
{
NSError *error = nil;
NSObject *object = [SFHFKeychainUtils getPasswordForUsername:key
andServiceName:@"MKStoreKit"
error:&error];
if(error)
NSLog(@"%@", [error localizedDescription]);
return object;
}
+(NSNumber*) numberForKey:(NSString*) key
{
return [NSNumber numberWithInt:[[MKStoreManager objectForKey:key] intValue]];
}
+(NSData*) dataForKey:(NSString*) key
{
NSString *str = [MKStoreManager objectForKey:key];
return [str dataUsingEncoding:NSUTF8StringEncoding];
}
#pragma mark Singleton Methods
+ (MKStoreManager*)sharedManager
{
if(!_sharedStoreManager) {
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedStoreManager = [[super allocWithZone:nil] init];
});
#if TARGET_IPHONE_SIMULATOR
NSLog(@"You are running in Simulator MKStoreKit runs only on devices");
#else
_sharedStoreManager = [[self alloc] init];
_sharedStoreManager.purchasableObjects = [[NSMutableArray alloc] init];
[_sharedStoreManager requestProductData];
_sharedStoreManager.storeObserver = [[MKStoreObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:_sharedStoreManager.storeObserver];
[_sharedStoreManager startVerifyingSubscriptionReceipts];
if([self iCloudAvailable])
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateFromiCloud:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:nil];
#endif
}
return _sharedStoreManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedManager];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
#pragma mark Internal MKStoreKit functions
-(NSDictionary*) storeKitItems
{
return [NSDictionary dictionaryWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
@"MKStoreKitConfigs.plist"]];
}
- (void) restorePreviousTransactionsOnComplete:(void (^)(void)) completionBlock
onError:(void (^)(NSError*)) errorBlock
{
self.onRestoreCompleted = completionBlock;
self.onRestoreFailed = errorBlock;
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void) restoreCompleted
{
if(self.onRestoreCompleted)
self.onRestoreCompleted();
self.onRestoreCompleted = nil;
}
-(void) restoreFailedWithError:(NSError*) error
{
if(self.onRestoreFailed)
self.onRestoreFailed(error);
self.onRestoreFailed = nil;
}
-(void) requestProductData
{
NSMutableArray *productsArray = [NSMutableArray array];
NSArray *consumables = [[[self storeKitItems] objectForKey:@"Consumables"] allKeys];
NSArray *nonConsumables = [[self storeKitItems] objectForKey:@"Non-Consumables"];
NSArray *subscriptions = [[[self storeKitItems] objectForKey:@"Subscriptions"] allKeys];
[productsArray addObjectsFromArray:consumables];
[productsArray addObjectsFromArray:nonConsumables];
[productsArray addObjectsFromArray:subscriptions];
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productsArray]];
request.delegate = self;
[request start];
}
- (BOOL) removeAllKeychainData {
NSMutableArray *productsArray = [NSMutableArray array];
NSArray *consumables = [[[self storeKitItems] objectForKey:@"Consumables"] allKeys];
NSArray *nonConsumables = [[self storeKitItems] objectForKey:@"Non-Consumables"];
NSArray *subscriptions = [[[self storeKitItems] objectForKey:@"Subscriptions"] allKeys];
[productsArray addObjectsFromArray:consumables];
[productsArray addObjectsFromArray:nonConsumables];
[productsArray addObjectsFromArray:subscriptions];
int itemCount = productsArray.count;
NSError *error;
//loop through all the saved keychain data and remove it
for (int i = 0; i < itemCount; i++ ) {
[SFHFKeychainUtils deleteItemForUsername:[productsArray objectAtIndex:i] andServiceName:@"MKStoreKit" error:&error];
}
if (!error) {
return YES;
}
else {
return NO;
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
[self.purchasableObjects addObjectsFromArray:response.products];
#ifndef NDEBUG
for(int i=0;i<[self.purchasableObjects count];i++)
{
SKProduct *product = [self.purchasableObjects objectAtIndex:i];
NSLog(@"Feature: %@, Cost: %f, ID: %@",[product localizedTitle],
[[product price] doubleValue], [product productIdentifier]);
}
for(NSString *invalidProduct in response.invalidProductIdentifiers)
NSLog(@"Problem in iTunes connect configuration for product: %@", invalidProduct);
#endif
isProductsAvailable = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kProductFetchedNotification
object:[NSNumber numberWithBool:isProductsAvailable]];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
isProductsAvailable = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:kProductFetchedNotification
object:[NSNumber numberWithBool:isProductsAvailable]];
}
// call this function to check if the user has already purchased your feature
+ (BOOL) isFeaturePurchased:(NSString*) featureId
{
return [[MKStoreManager numberForKey:featureId] boolValue];
}
- (BOOL) isSubscriptionActive:(NSString*) featureId
{
MKSKSubscriptionProduct *subscriptionProduct = [self.subscriptionProducts objectForKey:featureId];
return [subscriptionProduct isSubscriptionActive];
}
// Call this function to populate your UI
// this function automatically formats the currency based on the user's locale
- (NSMutableArray*) purchasableObjectsDescription
{
NSMutableArray *productDescriptions = [[NSMutableArray alloc] initWithCapacity:[self.purchasableObjects count]];
for(int i=0;i<[self.purchasableObjects count];i++)
{
SKProduct *product = [self.purchasableObjects objectAtIndex:i];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
// you might probably need to change this line to suit your UI needs
NSString *description = [NSString stringWithFormat:@"%@ (%@)",[product localizedTitle], formattedString];
#ifndef NDEBUG
NSLog(@"Product %d - %@", i, description);
#endif
[productDescriptions addObject: description];
}
return productDescriptions;
}
/*Call this function to get a dictionary with all prices of all your product identifers
For example,
NSDictionary *prices = [[MKStoreManager sharedManager] pricesDictionary];
NSString *upgradePrice = [prices objectForKey:@"com.mycompany.upgrade"]
*/
- (NSMutableDictionary *)pricesDictionary {
NSMutableDictionary *priceDict = [NSMutableDictionary dictionary];
for(int i=0;i<[self.purchasableObjects count];i++)
{
SKProduct *product = [self.purchasableObjects objectAtIndex:i];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
NSString *priceString = [NSString stringWithFormat:@"%@", formattedString];
[priceDict setObject:priceString forKey:product.productIdentifier];
}
return priceDict;
}
-(void) showAlertWithTitle:(NSString*) title message:(NSString*) message {
#if TARGET_OS_IPHONE
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"Dismiss", @"")
otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_MAC
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:NSLocalizedString(@"Dismiss", @"")];
[alert setMessageText:title];
[alert setInformativeText:message];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert runModal];
#endif
}
- (void) buyFeature:(NSString*) featureId
onComplete:(void (^)(NSString*)) completionBlock
onCancelled:(void (^)(void)) cancelBlock
{
self.onTransactionCompleted = completionBlock;
self.onTransactionCancelled = cancelBlock;
[MKSKProduct verifyProductForReviewAccess:featureId
onComplete:^(NSNumber * isAllowed)
{
if([isAllowed boolValue])
{
[self showAlertWithTitle:NSLocalizedString(@"Review request approved", @"")
message:NSLocalizedString(@"You can use this feature for reviewing the app.", @"")];
if(self.onTransactionCompleted)
self.onTransactionCompleted(featureId);
}
else
{
[self addToQueue:featureId];
}
}
onError:^(NSError* error)
{
NSLog(@"Review request cannot be checked now: %@", [error description]);
[self addToQueue:featureId];
}];
}
-(void) addToQueue:(NSString*) productId
{
if ([SKPaymentQueue canMakePayments])
{
NSArray *allIds = [self.purchasableObjects valueForKey:@"productIdentifier"];
int index = [allIds indexOfObject:productId];
if(index == NSNotFound) return;
SKProduct *thisProduct = [self.purchasableObjects objectAtIndex:index];
SKPayment *payment = [SKPayment paymentWithProduct:thisProduct];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else
{
[self showAlertWithTitle:NSLocalizedString(@"In-App Purchasing disabled", @"")
message:NSLocalizedString(@"Check your parental control settings and try again later", @"")];
}
}
- (BOOL) canConsumeProduct:(NSString*) productIdentifier
{
int count = [[MKStoreManager numberForKey:productIdentifier] intValue];
return (count > 0);
}
- (BOOL) canConsumeProduct:(NSString*) productIdentifier quantity:(int) quantity
{
int count = [[MKStoreManager numberForKey:productIdentifier] intValue];
return (count >= quantity);
}
- (BOOL) consumeProduct:(NSString*) productIdentifier quantity:(int) quantity
{
int count = [[MKStoreManager numberForKey:productIdentifier] intValue];
if(count < quantity)
{
return NO;
}
else
{
count -= quantity;
[MKStoreManager setObject:[NSNumber numberWithInt:count] forKey:productIdentifier];
return YES;
}
}
- (void) startVerifyingSubscriptionReceipts
{
NSDictionary *subscriptions = [[self storeKitItems] objectForKey:@"Subscriptions"];
self.subscriptionProducts = [NSMutableDictionary dictionary];
for(NSString *productId in [subscriptions allKeys])
{
MKSKSubscriptionProduct *product = [[MKSKSubscriptionProduct alloc] initWithProductId:productId subscriptionDays:[[subscriptions objectForKey:productId] intValue]];
product.receipt = [MKStoreManager dataForKey:productId]; // cached receipt
if(product.receipt)
{
[product verifyReceiptOnComplete:^(NSNumber* isActive)
{
if([isActive boolValue] == NO)
{
[[NSNotificationCenter defaultCenter] postNotificationName:kSubscriptionsInvalidNotification
object:product.productId];
NSLog(@"Subscription: %@ is inactive", product.productId);
}
else
{
NSLog(@"Subscription: %@ is active", product.productId);
}
}
onError:^(NSError* error)
{
NSLog(@"Unable to check for subscription validity right now");
}];
}
[self.subscriptionProducts setObject:product forKey:productId];
}
}
-(NSData*) receiptFromBundle {
return nil;
}
#pragma mark In-App purchases callbacks
// In most cases you don't have to touch these methods
-(void) provideContent: (NSString*) productIdentifier
forReceipt:(NSData*) receiptData
{
MKSKSubscriptionProduct *subscriptionProduct = [self.subscriptionProducts objectForKey:productIdentifier];
if(subscriptionProduct)
{
// MAC In App Purchases can never be a subscription product (at least as on Dec 2011)
// so this can be safely ignored.
subscriptionProduct.receipt = receiptData;
[subscriptionProduct verifyReceiptOnComplete:^(NSNumber* isActive)
{
[[NSNotificationCenter defaultCenter] postNotificationName:kSubscriptionsPurchasedNotification
object:productIdentifier];
[MKStoreManager setObject:receiptData forKey:productIdentifier];
}
onError:^(NSError* error)
{
NSLog(@"%@", [error description]);
}];
}
else
{
if(!receiptData) {
// could be a mac in app receipt.
// read from receipts and verify here
receiptData = [self receiptFromBundle];
if(!receiptData) {
if(self.onTransactionCancelled)
{
self.onTransactionCancelled(productIdentifier);
}
else
{
NSLog(@"Receipt invalid");
}
}
}
if(OWN_SERVER && SERVER_PRODUCT_MODEL)
{
// ping server and get response before serializing the product
// this is a blocking call to post receipt data to your server
// it should normally take a couple of seconds on a good 3G connection
MKSKProduct *thisProduct = [[MKSKProduct alloc] initWithProductId:productIdentifier receiptData:receiptData];
[thisProduct verifyReceiptOnComplete:^
{
[self rememberPurchaseOfProduct:productIdentifier];
}
onError:^(NSError* error)
{
if(self.onTransactionCancelled)
{
self.onTransactionCancelled(productIdentifier);
}
else
{
NSLog(@"The receipt could not be verified");
}
}];
}
else
{
[self rememberPurchaseOfProduct:productIdentifier];
if(self.onTransactionCompleted)
self.onTransactionCompleted(productIdentifier);
}
}
}
-(void) rememberPurchaseOfProduct:(NSString*) productIdentifier
{
NSDictionary *allConsumables = [[self storeKitItems] objectForKey:@"Consumables"];
if([[allConsumables allKeys] containsObject:productIdentifier])
{
NSDictionary *thisConsumableDict = [allConsumables objectForKey:productIdentifier];
int quantityPurchased = [[thisConsumableDict objectForKey:@"Count"] intValue];
NSString* productPurchased = [thisConsumableDict objectForKey:@"Name"];
int oldCount = [[MKStoreManager numberForKey:productPurchased] intValue];
int newCount = oldCount + quantityPurchased;
[MKStoreManager setObject:[NSNumber numberWithInt:newCount] forKey:productPurchased];
}
else
{
[MKStoreManager setObject:[NSNumber numberWithBool:YES] forKey:productIdentifier];
}
}
- (void) transactionCanceled: (SKPaymentTransaction *)transaction
{
#ifndef NDEBUG
NSLog(@"User cancelled transaction: %@", [transaction description]);
NSLog(@"error: %@", transaction.error);
#endif
if(self.onTransactionCancelled)
self.onTransactionCancelled();
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
#ifndef NDEBUG
NSLog(@"Failed transaction: %@", [transaction description]);
NSLog(@"error: %@", transaction.error);
#endif
[self showAlertWithTitle:[transaction.error localizedFailureReason] message:[transaction.error localizedRecoverySuggestion]];
if(self.onTransactionCancelled)
self.onTransactionCancelled();
}
@end