-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRootViewController.m
More file actions
432 lines (293 loc) · 13.5 KB
/
RootViewController.m
File metadata and controls
432 lines (293 loc) · 13.5 KB
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
#import "RootViewController.h"
#import "InAppRageIAPHelper.h"
#import "Reachability.h"
@implementation RootViewController
{
NSArray *arr;
}
@synthesize hud = _hud;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
// self.title = @"Vip";
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
// [btn setTitle:@"恢复购买" forState:UIControlStateNormal];
// btn.backgroundColor = [UIColor purpleColor];
//
// [btn addTarget:self action:@selector(restore:) forControlEvents:UIControlEventTouchUpInside];
//
// UIBarButtonItem*RightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
// self.navigationItem.rightBarButtonItem= RightItem;
}
- (void)restore:(UIButton *)btn
{
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"恢复已购买会员"
message:@"您要恢复之前购买过的会员类型吗?"
delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
alerView.tag = 111;
[alerView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 111)
{
if(buttonIndex == 1)
{
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
NSLog(@"13");
}
}
}
- (void)dismissHUD:(id)arg {
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
self.hud = nil;
}
- (void)productsLoaded:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
TableView.hidden = FALSE;
NSLog(@"1");
[TableView reloadData];
}
- (void)timeout:(id)arg {
_hud.labelText = @"请求超时!";
_hud.detailsLabelText = @"请稍后重试";
_hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
_hud.mode = MBProgressHUDModeCustomView;
[self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:3.0];
}
- (void)updateInterfaceWithReachability: (Reachability*) curReach {
}
- (void)viewWillDisappear:(BOOL)animated
{
// [InAppRageIAPHelper sharedHelper].products = nil;
self.navigationController.navigationBarHidden =NO;
}
- (void)viewWillAppear:(BOOL)animated {
TableView = [[UITableView alloc]initWithFrame:CGRectMake(0,iOS7?64:64, KscreenWidth, KscreenHeight) style:UITableViewStylePlain];
// TableView.ba = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
TableView.delegate = self;
TableView.dataSource = self;
TableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:TableView];
//自定义导航条
UIView *nav = [self myNavgationBar:CGRectMake(0,iOS7?20:0, KscreenWidth, 44) andTitle:@"vip"];
[self.view addSubview:nav];
self.navigationController.navigationBarHidden =YES;
TableView.hidden = TRUE;
[[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];
if ([SKPaymentQueue canMakePayments]) {
//[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(@"No internet connection!");
} else {
// if ([InAppRageIAPHelper sharedHelper].products == nil) {
[[InAppRageIAPHelper sharedHelper] requestProducts];
self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
_hud.labelText = @"正在连接...";
[self performSelector:@selector(timeout:) withObject:nil afterDelay:30.0];
// }
}
}
else
{
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
message:@"你没允许应用程序内购买"
delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alerView show];
}
[super viewWillAppear:animated];
}
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[InAppRageIAPHelper 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];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KscreenWidth, 60)];
imgView.backgroundColor = KCOLOR(240, 240, 240, 1);
[cell.contentView addSubview:imgView];
}
// Configure the cell.
SKProduct *product = [[InAppRageIAPHelper 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;
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
UserInfo *userinfo = [UserInfo shareUserInfo];
if ([[InAppRageIAPHelper sharedHelper].purchasedProducts containsObject:product.productIdentifier] || userinfo.ifbuy.length>0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = nil;
} else {
UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
buyButton.frame = CGRectMake(0, 0, 72, 37);
[buyButton setTitle:@"购买" forState:UIControlStateNormal];
[buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
buyButton.backgroundColor = KCOLOR(26, 148, 223, 1);
buyButton.layer.cornerRadius = 3;
buyButton.layer.masksToBounds = YES;
buyButton.tag = indexPath.row;
[buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2)
{
}
}
#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)buyButtonTapped:(id)sender {
UIButton *buyButton = (UIButton *)sender;
SKProduct *product = [[InAppRageIAPHelper sharedHelper].products objectAtIndex:buyButton.tag];
NSLog(@"Buying %@...", product.productIdentifier);
[[InAppRageIAPHelper sharedHelper] buyProductIdentifier:product.productIdentifier];
self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
_hud.labelText = @"正在连接...";
[self performSelector:@selector(timeout:) withObject:nil afterDelay:60*5];
}
- (void)productPurchased:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
NSString *productIdentifier = (NSString *) notification.object;
NSLog(@"-----Purchased: %@", productIdentifier);
NSLog(@"2");
[TableView reloadData];
}
- (void)productPurchaseFailed:(NSNotification *)notification {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
SKPaymentTransaction * transaction = (SKPaymentTransaction *) notification.object;
if (transaction.error.code != SKErrorPaymentCancelled) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:transaction.error.localizedDescription
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert show];
}
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.hud = nil;
TableView = nil;
}
#pragma mark 自定义导航条
-(UIView *)myNavgationBar:(CGRect)rect andTitle:(NSString *)tit
{
UIView *view = [[UIView alloc] initWithFrame:rect];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"title.png"]];
//back button
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(2, 0, 40, rect.size.height);
[back addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:@"title_icon.png"] forState:UIControlStateNormal];
[view addSubview:back];
//title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(back.frame.size.width, 0, 70, rect.size.height)];
title.text = tit;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor colorWithRed:0.92f green:0.92f blue:0.92f alpha:1.00f];
[view addSubview:title];
return view;
}
-(void)backBtnClick:(UIButton *)btn
{
[self.navigationController popViewControllerAnimated:YES];
}
@end