-
Notifications
You must be signed in to change notification settings - Fork 1
/
IAPHelper.m
135 lines (97 loc) · 4.12 KB
/
IAPHelper.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
//
// IAPHelper.m
// GolfSwingMeter
//
// Created by Liangjun Jiang on 7/20/12.
// Copyright (c) 2012 ByPass Lane. All rights reserved.
//
#import "IAPHelper.h"
@implementation IAPHelper
@synthesize productIdentifier = _produtIdentifiers;
@synthesize products = _products;
@synthesize purchasedProducts = _purchasedProducts;
@synthesize request = _request;
- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers
{
if ((self = [super init])) {
_produtIdentifiers = productIdentifiers;
// Check for Previous Purchased products
NSMutableSet *purchasedProducts = [NSMutableSet set];
[_produtIdentifiers enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:obj];
if (productPurchased) {
[purchasedProducts addObject:obj];
DebugLog(@"purchased: %@",obj);
}
DebugLog(@"not purchased: %@",obj);
}];
self.purchasedProducts = purchasedProducts;
}
return self;
}
- (void)requestProducts{
self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:_produtIdentifiers];
_request.delegate = self;
[_request start];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
self.products = response.products;
self.request = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:_products];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
DebugLog(@"what's the error :%@",error.localizedDescription);
}
- (void)buyProductIdentifier:(NSString *)productIdentifier {
DebugLog(@"Buying %@...", productIdentifier);
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
# pragma mark - delegate methods
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
// Optional: Record the transaction on the server side...
}
- (void)provideContent: (NSString *)productIdentifier {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
[_purchasedProducts addObject:productIdentifier];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
DebugLog(@"Complete transaction");
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
DebugLog(@"restore transaction");
[self recordTransaction:transaction];
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
if (transaction.error.code != SKErrorPaymentCancelled) {
DebugLog(@"Transaction error: %@",transaction.error.localizedDescription);
}
[[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchaseFailedNotification object:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
[transactions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SKPaymentTransaction *transaction = (SKPaymentTransaction *)obj;
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}];
}
@end