猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!
昨天使用ios7SDK build的时候,发现了几个warning,原来以前写的内购方法,有些在ios7下弃用了。今天改了下,顺便代码也发上。
PGStoreKitManager.h
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
|
//
// PGStoreKitManager.h
// OCPhysicGame
//
// Created by LiuYanghui on 14-1-26.
// Copyright (c) 2014年 LiuYanghui. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
#import <StoreKit/StoreKit.h>
@
class
ViewController
;
@
interface
PGStoreKitManager
:
NSObject
<
GKGameCenterControllerDelegate
,
SKPaymentTransactionObserver
,
SKProductsRequestDelegate
>
{
UIAlertView *
_loadingAlert
;
BOOL
_enableGameCenter
;
}
@
property
(
nonatomic
,
readwrite
,
strong
)
ViewController*
viewController
;
+
(
PGStoreKitManager *
)
getInstance
;
// game center ----------------------------------------
/**
登陆gamecenter,请先设置setViewController
*/
-
(
void
)
authenticateLocalPlayer
;
/**
上传积分
*/
-
(
void
)
reportScore
:
(
NSString*
)
identifier
hiScore
:
(
int64_t
)
score
;
/**
上传成就
*/
-
(
void
)
reportAchievementIdentifier
:
(
NSString*
)
identifier
percentComplete
:
(
float
)
percent
;
/**
显示排行版
*/
-
(
void
)
showLeaderboard
:
(
NSString*
)
leaderboard
;
/**
显示成就
*/
-
(
void
)
showAchievements
;
// iap ----------------------------------------
/**
初始化内消费
*/
-
(
void
)
initStoreKit
;
/**
购买产品
*/
-
(
void
)
purchaseItem
:
(
NSString*
)
identifier
;
@
end
|
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"
@
implementation
PGStoreKitManager
+
(
PGStoreKitManager *
)
getInstance
{
static
PGStoreKitManager *
mgr
=
nil
;
static
dispatch_once_t
onceToken
;
dispatch_once
(
&
onceToken
,
^
{
mgr
=
[
[
self
alloc
]
init
]
;
}
)
;
return
mgr
;
}
-
(
id
)
init
{
self
=
[
super
init
]
;
if
(
self
)
{
[
self
initData
]
;
return
self
;
}
return
nil
;
}
-
(
void
)
initData
{
_enableGameCenter
=
NO
;
_viewController
=
nil
;
}
-
(
void
)
showMessage
:
(
NSString *
)
title
Message
:
(
NSString *
)
msg
{
UIAlertView *
alert
=
[
[
UIAlertView
alloc
]
initWithTitle
:
title
message
:
msg
delegate
:
self
cancelButtonTitle
:
@
"确定"
otherButtonTitles
:
nil
,
nil
]
;
[
alert
show
]
;
}
-
(
void
)
showLoadingView
:
(
NSString *
)
title
{
_loadingAlert
=
[
[
UIAlertView
alloc
]
initWithTitle
:
title
message
:
@
""
delegate
:
self
cancelButtonTitle
:
nil
otherButtonTitles
:
nil
,
nil
]
;
[
_loadingAlert
show
]
;
}
-
(
void
)
removeLoadingView
{
[
_loadingAlert
dismissWithClickedButtonIndex
:
0
animated
:
YES
]
;
}
#pragma mark - GameCenter
-
(
void
)
authenticateLocalPlayer
{
GKLocalPlayer*
localPlayer
=
[
GKLocalPlayer
localPlayer
]
;
if
(
[
localPlayer
isAuthenticated
]
==
NO
)
{
localPlayer
.
authenticateHandler
=
^
(
UIViewController *
viewController
,
NSError *
error
)
{
if
(
error
)
{
_enableGameCenter
=
NO
;
}
else
{
_enableGameCenter
=
YES
;
if
(
viewController
)
{
[
_viewController
presentViewController
:
viewController
animated
:
YES
completion
:
nil
]
;
}
}
}
;
}
else
{
_enableGameCenter
=
YES
;
}
}
/**
上传积分
*/
-
(
void
)
reportScore
:
(
NSString*
)
identifier
hiScore
:
(
int64_t
)
score
;
{
if
(
score
<
0
||
!
_enableGameCenter
)
return
;
GKScore *
scoreBoard
=
[
[
GKScore
alloc
]
initWithLeaderboardIdentifier
:
identifier
]
;
scoreBoard
.
value
=
score
;
[
GKScore
reportScores
:
@
[
scoreBoard
]
withCompletionHandler
:
^
(
NSError *
error
)
{
if
(
error
)
{
// handle error
}
}
]
;
}
/**
上传成就
*/
-
(
void
)
reportAchievementIdentifier
:
(
NSString*
)
identifier
percentComplete
:
(
float
)
percent
{
if
(
percent
<
0
||
!
_enableGameCenter
)
return
;
GKAchievement *
achievement
=
[
[
GKAchievement
alloc
]
initWithIdentifier
:
identifier
]
;
if
(
achievement
)
{
achievement
.
percentComplete
=
percent
;
[
GKAchievement
reportAchievements
:
@
[
achievement
]
withCompletionHandler
:
^
(
NSError *
error
)
{
if
(
error
)
{
// handle error
}
}
]
;
}
}
/**
显示排行版
*/
-
(
void
)
showLeaderboard
:
(
NSString*
)
leaderboard
{
if
(
!
_enableGameCenter
)
return
;
GKGameCenterViewController *
gameCenterViewController
=
[
[
GKGameCenterViewController
alloc
]
init
]
;
gameCenterViewController
.
viewState
=
GKGameCenterViewControllerStateLeaderboards
;
gameCenterViewController
.
gameCenterDelegate
=
self
;
[
_viewController
presentViewController
:
gameCenterViewController
animated
:
YES
completion
:
nil
]
;
}
/**
显示成就
*/
-
(
void
)
showAchievements
{
if
(
!
_enableGameCenter
)
return
;
GKGameCenterViewController *
gameCenterViewController
=
[
[
GKGameCenterViewController
alloc
]
init
]
;
gameCenterViewController
.
viewState
=
GKGameCenterViewControllerStateAchievements
;
gameCenterViewController
.
gameCenterDelegate
=
self
;
[
_viewController
presentViewController
:
gameCenterViewController
animated
:
YES
completion
:
nil
]
;
}
#pragma mark gameCenterViewController Close回调
-
(
void
)
gameCenterViewControllerDidFinish
:
(
GKGameCenterViewController *
)
gameCenterViewController
{
[
_viewController
dismissViewControllerAnimated
:
YES
completion
:
nil
]
;
}
//---------------------------------------------------------
#pragma mark - IAP
-
(
BOOL
)
canProcessPayments
{
if
(
[
SKPaymentQueue
canMakePayments
]
)
{
return
YES
;
}
else
{
return
NO
;
}
}
/**
初始化内消费
*/
-
(
void
)
initStoreKit
{
[
[
SKPaymentQueue
defaultQueue
]
addTransactionObserver
:
self
]
;
}
/**
购买产品
*/
-
(
void
)
purchaseItem
:
(
NSString *
)
identifier
{
[
self
showLoadingView
:
@
"Access Store..."
]
;
if
(
!
[
self
canProcessPayments
]
)
{
NSLog
(
@
"1.失败-->SKPaymentQueue canMakePayments NO"
)
;
[
self
removeLoadingView
]
;
return
;
}
NSLog
(
@
"1.成功-->请求产品信息...%@"
,
identifier
)
;
// 使用请求商品信息式购买
SKProductsRequest *
request
=
[
[
SKProductsRequest
alloc
]
initWithProductIdentifiers
:
[
NSSet
setWithObject
:
identifier
]
]
;
request
.
delegate
=
self
;
[
request
start
]
;
}
// SKProductsRequest 的回调
-
(
void
)
productsRequest
:
(
SKProductsRequest *
)
request
didReceiveResponse
:
(
SKProductsResponse *
)
response
{
NSArray *
myProduct
=
response
.
products
;
if
(
myProduct
.
count
==
0
)
{
NSLog
(
@
"2.失败-->无法获取产品信息,购买失败。invalidProductIdentifiers = %@"
,
response
.
invalidProductIdentifiers
)
;
[
self
removeLoadingView
]
;
return
;
}
NSLog
(
@
"2.成功-->获取产品信息成功,正在购买..."
)
;
SKPayment *
payment
=
[
SKPayment
paymentWithProduct
:
myProduct
[
0
]
]
;
[
[
SKPaymentQueue
defaultQueue
]
addPayment
:
payment
]
;
}
// SKPayment 的回调
-
(
void
)
paymentQueue
:
(
SKPaymentQueue *
)
queue
updatedTransactions
:
(
NSArray *
)
transactions
{
NSLog
(
@
"3.成功-->接收苹果购买数据,正在处理..."
)
;
for
(
SKPaymentTransaction *
transaction
in
transactions
)
{
switch
(
transaction
.
transactionState
)
{
case
SKPaymentTransactionStatePurchased
:
[
self
completeTransaction
:
transaction
]
;
break
;
case
SKPaymentTransactionStateFailed
:
[
self
failedTransaction
:
transaction
]
;
break
;
case
SKPaymentTransactionStateRestored
:
[
self
restoreTransaction
:
transaction
]
;
break
;
default
:
break
;
}
}
}
// 结束交易
-
(
void
)
completeTransaction
:
(
SKPaymentTransaction*
)
transaction
{
NSLog
(
@
"4.成功-->结束交易 SKPaymentTransactionStatePurchased"
)
;
[
self
removeLoadingView
]
;
// 记录交易和提供产品 这两方法必须处理
[
self
recordTransaction
:
transaction
]
;
[
self
provideContent
:
transaction
.
payment
.
productIdentifier
]
;
// 移除 transaction from the payment queue.
[
[
SKPaymentQueue
defaultQueue
]
finishTransaction
:
transaction
]
;
}
// 重置交易
-
(
void
)
restoreTransaction
:
(
SKPaymentTransaction*
)
transaction
{
NSLog
(
@
"4.成功-->重置交易 SKPaymentTransactionStateRestored"
)
;
[
self
recordTransaction
:
transaction
]
;
[
self
provideContent
:
transaction
.
originalTransaction
.
payment
.
productIdentifier
]
;
[
[
SKPaymentQueue
defaultQueue
]
finishTransaction
:
transaction
]
;
}
// 交易失败
-
(
void
)
failedTransaction
:
(
SKPaymentTransaction*
)
transaction
{
[
self
removeLoadingView
]
;
NSLog
(
@
"4.成功-->交易失败 SKPaymentTransactionStateRestored error.code:%d"
,
(
int
)
transaction
.
error
.
code
)
;
[
[
SKPaymentQueue
defaultQueue
]
finishTransaction
:
transaction
]
;
}
// 交易记录
-
(
void
)
recordTransaction
:
(
SKPaymentTransaction*
)
transacation
{
NSLog
(
@
"4.成功-->交易记录, 可以在此处存储记录"
)
;
}
// 提供产品
-
(
void
)
provideContent
:
(
NSString*
)
identifier
{
NSLog
(
@
"4.成功-->交易成功,请提供产品 identifier = %@"
,
identifier
)
;
[
self
removeLoadingView
]
;
[
self
showMessage
:
@
"Success"
Message
:
@
"You have successfully purchased."
]
;
}
@
end
|
注释已经写的很清楚了,再有不清楚的可以留言。
- 本文固定链接: http://www.cocos2dev.com/?p=514