一,引入 storekit.framework 和 #import并且实现代理SKProductsRequestDelegate,SKPaymentTransactionObserver
二,在自己服务器上面获得商品信息,也可以向appStore发送请求询问产品信息。
在向appStore后台发送请求之前需要判断当前设备是否支持购买:(此段代码一般写在ViewDidLoad)
{
if ([self isAllowPayMent]) {
NSLog(@"app 用户允许支付");
[self getProductInfo];
}else{
NSLog(@"app 用户不允许支付");
}
// 监听购买结果
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
//判断是否支持支付的方法
- (BOOL)isAllowPayMent
{
return [SKPaymentQueue canMakePayments];
}
//请求appStore产品信息
- (void)getProductInfo
{
NSSet * set = [NSSet setWithObjects:@"XXXX",@"XXXX",@"XXXX",nil];(set集里面为开发中中心后台审核通过的产品名称)
SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
request.delegate = self;
[request start];
}
//SKProductsRequest的返回代理方法
-(void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *myProduct = response.products;
if (myProduct.count == 0) {
NSLog(@"无法获取产品信息,购买失败。");
return;
}
//SKProduct *product in response.products
for (int i = 0; i < response.products.count; i ++) {
// 激活了对应的销售操作按钮,相当于商店的商品上架允许销售
SKProduct *product = [response.products objectAtIndex:i];
NSLog(@"%@", product.productIdentifier);
UIButton * btn = [btnArr objectAtIndex:i];(btnArr为存放产品按钮的数组)
btn.userInteractionEnabled = YES;(只有当请求得到数据之后才放开产品按钮的交互性)
// 填充商品数组
[productArr_app addObject:product];(productArr_app为自己生成的用于存放产品的数组)
}
}
三,用户决定购买商品,在商品按钮的点击方法中调用此方法
- (void)buyProduct:(SKProduct *)product
{
SKPayment *payment = [SKPayment paymentWithProduct:product];
// 准备购买(异步)
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
四,监听购买结果,当交易完成的时候进行下一步操作,即验证购买合法化
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchased://交易完成
NSLog(@"交易完成transactionIdentifier = %@", t ransaction.transactionIdentifier);
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed://交易失败
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored://已经购买过该商品
[self restoreTransaction:transaction];
break;
case SKPaymentTransactionStatePurchasing: //商品添加进列表
NSLog(@"商品添加进列表");
break;
default:
break;
}
}
}
五,完成交易,向自己的服务器验证交易合法化
-(void)completeTransaction:(SKPaymentTransaction *)transaction {
NSString * productIdentifier = transaction.payment.productIdentifier;
if ([productIdentifier length] > 0) {
// 向自己的服务器验证购买凭证
//appStoreReceiptURL iOS7.0增加的,购买交易完成后,会将凭据存放在该地址
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
// 从沙盒中获取到购买凭据
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
// 传输的是BASE64编码的字符串
NSString *encodeStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}
//接下来就是向自己的服务器发送数据验证合法化的步骤了
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
(在公司的测试服务器中,连接苹果的测试服务器(https://sandbox.itunes.apple.com/verifyReceipt)验证。部署在线上的正式服务器中,就要连接苹果的正式服务器(https://buy.itunes.apple.com/verifyReceipt )验证了。)
六,下面是交易失败时候的方法
-(void)failedTransaction:(SKPaymentTransaction *)transaction {
if(transaction.error.code!= SKErrorPaymentCancelled) {
if (!fault_alert) {
fault_alert = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"购买失败,请确定您的apple id余额足够或绑定了银联卡消费" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
[fault_alert show];
}else{
}
} else {
NSLog(@"用户取消交易");
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(void)restoreTransaction:(SKPaymentTransaction *)transaction {
// 对于已购商品,处理恢复购买的逻辑
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}