App内购 Code 8步流程
2017-02-08 本文已影响331人
HunterDude
注意: 本文,只写大体重要几个步骤,不是很详细。
仔细研究建议观看:
In-App Purchase for Developer、
巧哥的(iOS应用内付费(IAP)开发步骤列表 、
Yi'mouleng、
如何正确使用IAP
等等......
创建你的 SkpayManager
导入 StoreKit库
签订<SKPaymentTransactionObserver, SKProductsRequestDelegate>
协议
SkpayManager:
1. 程序启动添加SKPay观察者,同时像后端请求产品列表信息
- (void)launch {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self requestProductList];
}
2. 程序结束后,移除SKpay观察者
- (void)terminate {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
3. 这时候已请求拿到我们服务端给的产品信息,购买页面可进行UI布局展示
UI展示4. 进入购买页面,通过用户选择的产品ID,以及quantity进行购买
- (void)buyProductsWithId:(NSString *)productsId andQuantity:(NSInteger)quantity {
self.productsId = productsId;
self.quantity = quantity;
if ([SKPaymentQueue canMakePayments]) {
//允许程序内付费购买
[self RequestProductData:@[self.productsId]];
} else {
[[ZToastManager ShardInstance] showtoast:@"您的手机没有打开程序内付费购买"];
}
}
5. 然后通过用户选中的产品ID数组,SKProductsRequest 开始请求购买
- (void)RequestProductData:(NSArray *)productsIdArr {
//请求对应的产品信息
NSSet *nsset = [NSSet setWithArray:productsIdArr];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}
6. SKProductsRequestDelegate 会接收到请求响应,在此回调中,发送购买请求
//收到的产品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
//收到产品反馈信息
NSArray *myProduct = response.products;
// NSLog(@"产品Product ID:%@", response.invalidProductIdentifiers);
// NSLog(@"产品付费数量: %d", (int) [myProduct count]);
// populate UI
for (SKProduct *product in myProduct) {
// NSLog(@"product info");
// NSLog(@" 基本描述: %@", [product description]);
// NSLog(@" IAP的id: %@", product.productIdentifier);
// NSLog(@" 地区编码: %@", product.priceLocale.localeIdentifier);
// NSLog(@" 本地价格: %@", product.price);
// NSLog(@" 语言代码: %@", [product.priceLocale objectForKey:NSLocaleLanguageCode]);
// NSLog(@" 国家代码: %@", [product.priceLocale objectForKey:NSLocaleCountryCode]);
// NSLog(@" 货币代码: %@", [product.priceLocale objectForKey:NSLocaleCurrencyCode]);
// NSLog(@" 货币符号: %@", [product.priceLocale objectForKey:NSLocaleCurgegrencySymbol]);
// NSLog(@" 本地标题: %@", product.localizedTitle);
// NSLog(@" 本地描述: %@", product.localizedDescription);
[self updateProductPriceWithId:product.productIdentifier andPrice:product.price];
if ([[product.priceLocale objectForKey:NSLocaleCurrencyCode] isEqualToString:@"CNY"]) {
self.CurrencyCode = @"¥";
} else {
self.CurrencyCode = [product.priceLocale objectForKey:NSLocaleCurrencySymbol];
}
}
//发送购买请求
for (SKProduct *prct in myProduct) {
if ([self.productsId isEqualToString:prct.productIdentifier]) {
SKMutablePayment *payment = nil;
payment = [SKMutablePayment paymentWithProduct:prct];
payment.quantity = self.quantity;
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
}
7. SKPaymentTransactionObserver 此协议会监听到购买结果,根据购买结果的不同,做出不同的逻辑。
#pragma mark - SKPaymentTransactionObserver
//----监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
//交易结果
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased: {
//交易完成
[self completeTransaction:transaction];
}
break;
case SKPaymentTransactionStateFailed: {
//交易失败
[self hideHud];
[self failedTransaction:transaction];
[[ZToastManager ShardInstance] showtoast:@"交易失败"];
}
break;
case SKPaymentTransactionStateRestored: {
//已经购买过该商品
[self hideHud];
[self restoreTransaction:transaction];
[[ZToastManager ShardInstance] showtoast:@"已经购买过该商品"];
}
break;
case SKPaymentTransactionStatePurchasing: {
//商品添加进列表
NSLog(@"商品添加进列表");
}
break;
case SKPaymentTransactionStateDeferred: {
NSLog(@"SKPayment Transaction State Deferred");
}
break;
default:
break;
}
}
}
8. 购买成功之后,需将receipt 上传至自己的服务端
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
// Your application should implement these two methods.
//NSString *product = transaction.payment.productIdentifier;
[self verifyReceipt:transaction];
}