-
Notifications
You must be signed in to change notification settings - Fork 1
/
InAppPurchaseViewController.m
213 lines (151 loc) · 7.13 KB
/
InAppPurchaseViewController.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
//
// RootViewController.m
// InAppRage
//
// Created by Liangjun Jiang on 2/28/11.
// Copyright 2012 LJSport Apps All rights reserved.
//
#import "InAppPurchaseViewController.h"
#import "InAppIAPHelper.h"
#import "Reachability.h"
#import "SVProgressHUD/SVProgressHUD.h"
@implementation InAppPurchaseViewController
@synthesize delegate, mTable;
- (IBAction)done:(id)sender
{
[SVProgressHUD dismiss];
[self.delegate purchaseControllerDidFinish:self];
}
#pragma mark -
#pragma mark View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productsLoaded:) name:kProductsLoadedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(productPurchased:) name:kProductPurchasedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(productPurchaseFailed:) name:kProductPurchaseFailedNotification object: nil];
}
return self;
}
- (void)viewDidLoad {
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
[SVProgressHUD showErrorWithStatus:@"No internet connection!"];
} else {
// NSLog(@"purchased: %@", [InAPPIAPHelper sharedHelper].purchasedProducts);
if ([InAPPIAPHelper sharedHelper].products == nil) {
[[InAPPIAPHelper sharedHelper] requestProducts];
[SVProgressHUD showWithStatus:@"Loading..."];
[self performSelector:@selector(timeout:) withObject:nil afterDelay:30.0];
}
}
}
- (void)dismissHUD:(id)arg {
[SVProgressHUD dismiss];
}
- (void)productsLoaded:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[SVProgressHUD dismiss];
self.mTable.hidden = NO;
[self.mTable reloadData];
}
- (void)timeout:(id)arg {
[SVProgressHUD showErrorWithStatus:@"Timeout! Please try again later. "];
[self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:3.0];
}
- (void)updateInterfaceWithReachability: (Reachability*) curReach {
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (section ==0)?1:[[InAPPIAPHelper sharedHelper].products count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.section ==0) {
UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buyButton.frame = CGRectMake(0, 0, 200, 37);
[buyButton setTitle:NSLocalizedString(@"Restore", @"Restore") forState:UIControlStateNormal];
buyButton.tag = indexPath.row;
[buyButton addTarget:self action:@selector(restoreButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton;
} else {
// Configure the cell.
SKProduct *product = [[InAPPIAPHelper sharedHelper].products objectAtIndex:indexPath.row];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedString = [numberFormatter stringFromNumber:product.price];
cell.textLabel.text = product.localizedTitle;
cell.detailTextLabel.text = formattedString;
if ([[InAPPIAPHelper sharedHelper].purchasedProducts containsObject:product.productIdentifier]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = nil;
} else {
UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buyButton.frame = CGRectMake(0, 0, 72, 37);
[buyButton setTitle:@"Buy" forState:UIControlStateNormal];
buyButton.tag = indexPath.row;
[buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (IBAction)restoreButtonTapped:(id)sender
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (IBAction)buyButtonTapped:(id)sender {
UIButton *buyButton = (UIButton *)sender;
SKProduct *product = [[InAPPIAPHelper sharedHelper].products objectAtIndex:buyButton.tag];
[[InAPPIAPHelper sharedHelper] buyProductIdentifier:product.productIdentifier];
[SVProgressHUD showWithStatus:@"Buying..."];
[self performSelector:@selector(timeout:) withObject:nil afterDelay:60];
}
- (void)productPurchased:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[SVProgressHUD dismiss];
// NSString *productIdentifier = (NSString *) notification.object;
// NSLog(@"Purchased: %@", productIdentifier);
[self.mTable reloadData];
}
- (void)productPurchaseFailed:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[SVProgressHUD dismiss];
SKPaymentTransaction * transaction = (SKPaymentTransaction *) notification.object;
if (transaction.error.code != SKErrorPaymentCancelled) {
[SVProgressHUD showErrorWithStatus:transaction.error.localizedDescription];
}
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:kProductPurchaseFailedNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kProductPurchasedNotification object:nil];
}
@end