ios7内购、Game Center 实现 in-App Purchases & Game Center
来源:互联网 发布:vb一维数组的定义方式 编辑:程序博客网 时间:2024/06/03 19:21
原文地址: http://www.cocos2dev.com/?p=514
昨天使用ios7SDK build的时候,发现了几个warning,原来以前写的内购方法,有些在ios7下弃用了。今天改了下,顺便代码也发上。
PGStoreKitManager.h
PGStoreKitManager.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
//
// PGStoreKitManager.m
// OCPhysicGame
//
// Created by LiuYanghui on 14-1-26.
// Copyright (c) 2014年 LiuYanghui. All rights reserved.
//
#import "PGStoreKitManager.h"
#import "ViewController.h"
@implementationPGStoreKitManager
+(PGStoreKitManager *)getInstance
{
staticPGStoreKitManager *mgr=nil;
staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
mgr=[[selfalloc]init];
});
returnmgr;
}
-(id)init
{
self=[superinit];
if(self){
[selfinitData];
returnself;
}
returnnil;
}
-(void)initData
{
_enableGameCenter=NO;
_viewController=nil;
}
-(void)showMessage:(NSString *)titleMessage:(NSString *)msg
{
UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:titlemessage:msgdelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertshow];
}
-(void)showLoadingView:(NSString *)title
{
_loadingAlert=[[UIAlertViewalloc]initWithTitle:titlemessage:@""delegate:selfcancelButtonTitle:nilotherButtonTitles:nil,nil];
[_loadingAlertshow];
}
-(void)removeLoadingView
{
[_loadingAlertdismissWithClickedButtonIndex:0animated:YES];
}
#pragma mark - GameCenter
-(void)authenticateLocalPlayer
{
GKLocalPlayer*localPlayer=[GKLocalPlayerlocalPlayer];
if([localPlayerisAuthenticated]==NO){
localPlayer.authenticateHandler=^(UIViewController *viewController,
NSError *error){
if(error){
_enableGameCenter=NO;
}else{
_enableGameCenter=YES;
if(viewController){
[_viewControllerpresentViewController:viewControlleranimated:YEScompletion:nil];
}
}
};
}else{
_enableGameCenter=YES;
}
}
/**
上传积分
*/
-(void)reportScore:(NSString*)identifierhiScore:(int64_t)score;
{
if(score<0||!_enableGameCenter)
return;
GKScore *scoreBoard=[[GKScorealloc]initWithLeaderboardIdentifier:identifier];
scoreBoard.value=score;
[GKScorereportScores:@[scoreBoard]withCompletionHandler:^(NSError *error){
if(error){
// handle error
}
}];
}
/**
上传成就
*/
-(void)reportAchievementIdentifier:(NSString*)identifierpercentComplete:(float)percent
{
if(percent<0||!_enableGameCenter)
return;
GKAchievement *achievement=[[GKAchievementalloc]initWithIdentifier:identifier];
if(achievement){
achievement.percentComplete=percent;
[GKAchievementreportAchievements:@[achievement]withCompletionHandler:^(NSError *error){
if(error){
// handle error
}
}];
}
}
/**
显示排行版
*/
-(void)showLeaderboard:(NSString*)leaderboard
{
if(!_enableGameCenter)
return;
GKGameCenterViewController *gameCenterViewController=[[GKGameCenterViewControlleralloc]init];
gameCenterViewController.viewState=GKGameCenterViewControllerStateLeaderboards;
gameCenterViewController.gameCenterDelegate=self;
[_viewControllerpresentViewController:gameCenterViewControlleranimated:YEScompletion:nil];
}
/**
显示成就
*/
-(void)showAchievements
{
if(!_enableGameCenter)
return;
GKGameCenterViewController *gameCenterViewController=[[GKGameCenterViewControlleralloc]init];
gameCenterViewController.viewState=GKGameCenterViewControllerStateAchievements;
gameCenterViewController.gameCenterDelegate=self;
[_viewControllerpresentViewController:gameCenterViewControlleranimated:YEScompletion:nil];
}
#pragma mark gameCenterViewController Close回调
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController
{
[_viewControllerdismissViewControllerAnimated:YEScompletion:nil];
}
//---------------------------------------------------------
#pragma mark - IAP
-(BOOL)canProcessPayments
{
if([SKPaymentQueuecanMakePayments]){
returnYES;
}else{
returnNO;
}
}
/**
初始化内消费
*/
-(void)initStoreKit
{
[[SKPaymentQueuedefaultQueue]addTransactionObserver:self];
}
/**
购买产品
*/
-(void)purchaseItem:(NSString *)identifier
{
[selfshowLoadingView:@"Access Store..."];
if(![selfcanProcessPayments]){
NSLog(@"1.失败-->SKPaymentQueue canMakePayments NO");
[selfremoveLoadingView];
return;
}
NSLog(@"1.成功-->请求产品信息...%@",identifier);
// 使用请求商品信息式购买
SKProductsRequest *request=[[SKProductsRequestalloc]
initWithProductIdentifiers:[NSSetsetWithObject:identifier]];
request.delegate=self;
[requeststart];
}
// SKProductsRequest 的回调
-(void)productsRequest:(SKProductsRequest *)requestdidReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct=response.products;
if(myProduct.count==0){
NSLog(@"2.失败-->无法获取产品信息,购买失败。invalidProductIdentifiers = %@",response.invalidProductIdentifiers);
[selfremoveLoadingView];
return;
}
NSLog(@"2.成功-->获取产品信息成功,正在购买...");
SKPayment *payment=[SKPaymentpaymentWithProduct:myProduct[0]];
[[SKPaymentQueuedefaultQueue]addPayment:payment];
}
// SKPayment 的回调
-(void)paymentQueue:(SKPaymentQueue *)queueupdatedTransactions:(NSArray *)transactions
{
NSLog(@"3.成功-->接收苹果购买数据,正在处理...");
for(SKPaymentTransaction *transactionintransactions){
switch(transaction.transactionState){
caseSKPaymentTransactionStatePurchased:
[selfcompleteTransaction:transaction];
break;
caseSKPaymentTransactionStateFailed:
[selffailedTransaction:transaction];
break;
caseSKPaymentTransactionStateRestored:
[selfrestoreTransaction:transaction];
break;
default:
break;
}
}
}
// 结束交易
-(void)completeTransaction:(SKPaymentTransaction*)transaction
{
NSLog(@"4.成功-->结束交易 SKPaymentTransactionStatePurchased");
[selfremoveLoadingView];
// 记录交易和提供产品 这两方法必须处理
[selfrecordTransaction:transaction];
[selfprovideContent:transaction.payment.productIdentifier];
// 移除 transaction from the payment queue.
[[SKPaymentQueuedefaultQueue]finishTransaction:transaction];
}
// 重置交易
-(void)restoreTransaction:(SKPaymentTransaction*)transaction
{
NSLog(@"4.成功-->重置交易 SKPaymentTransactionStateRestored");
[selfrecordTransaction:transaction];
[selfprovideContent:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueuedefaultQueue]finishTransaction:transaction];
}
// 交易失败
-(void)failedTransaction:(SKPaymentTransaction*)transaction
{
[selfremoveLoadingView];
NSLog(@"4.成功-->交易失败 SKPaymentTransactionStateRestored error.code:%d",(int)transaction.error.code);
[[SKPaymentQueuedefaultQueue]finishTransaction:transaction];
}
// 交易记录
-(void)recordTransaction:(SKPaymentTransaction*)transacation
{
NSLog(@"4.成功-->交易记录, 可以在此处存储记录");
}
// 提供产品
-(void)provideContent:(NSString*)identifier
{
NSLog(@"4.成功-->交易成功,请提供产品 identifier = %@",identifier);
[selfremoveLoadingView];
[selfshowMessage:@"Success"Message:@"You have successfully purchased."];
}
@end
注释已经写的很清楚了,再有不清楚的可以留言。
0 0
- ios7内购、Game Center 实现 in-App Purchases & Game Center
- ios7内购、Game Center 实现 in-App Purchases & Game Center
- Game Center
- App ID & Game Center - I
- game+center+demo
- Game Center简述
- Game Center Overview
- cocos2d-x Game Center
- 2dx Game Center
- iOS Game Center 登陆验证实现
- iOS 内购IAP(In-App Purchases)代码实现(上)
- iOS 内购IAP(In-App Purchases)代码实现(下)
- App ID & Game Center - II (代码部分)
- 2dx game center 简介
- ios Game-Center 开发 part1
- ios Game-Center 开发 part2
- [Terminal Game Center]弹球游戏
- [Terminal Game Center]贪吃蛇
- Android操作HTTP实现与服务器通信
- adb常用命令
- Java知识,你get到了吗
- 反编译问题
- bootstrap数字控件
- ios7内购、Game Center 实现 in-App Purchases & Game Center
- shell脚本一键安装mysql5.7.x
- Giraph集群测试Pagerank
- Java设计模式透析之 —— 单例(Singleton)
- mysql5.5更换配置文件my.ini后无法登录
- 【字符串处理算法】删除特定的字符的算法设计及C代码实现
- iOS之百度地图获取地理位置名称
- docker 数据管理<1>
- 微信统一订单