In-App Purchase
如果你的开发者账号中未进行过IAP相关设置,那么首先要进入iTunes Connect中,点击Contract, Tax, and Banking提交付费合约申请,需要完善的信息有:Contact Info, Bank Info, Tax Info. Bank info填写收款银行帐户。信息提交以后,审核会在24小时内完成,等这些信息都审核通过之后,你就可以进行开始实现IAP功能了。
我们首先需要在iTunes Connect中设置一些计费点,即product.
苹果支持5中product类型:
1.消耗类
2.非消耗类
3.自动订阅:
4.免费订阅
5非自动订阅
在开始购买流程之前,先要保证:
1.网络可用
Reachability *reach = [Reachability reachabilityInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable)
{
//Network disabled
}
2.购买权限
如果在设备的访问限制中心禁止了应用程序内购买,则不可进行内购
if (![SKPaymentQueue canMakePayments])
{
//IAP disabled
}
在此创建一个辅助类,遵循两个代理:SkProductsRequestDelegate, SKPaymentTransactionObserver
添加属性:
@property (retain) NSSet *productIdentifiers;
@property (retain) NSArray *products;
@property (retain) NSMutableSet *purchaseProducts;
@property (retain) SKProductsRequest *request;
由商品对应的Identifier获取商品信息(建议把所有商品的Identifier保存在服务器中,方便扩展)
- (void)requestProducts:(NSString *)productIdentifier
{
NSSet *productIdentifiers = [NSSet setWithObjects:productIdentifier, nil];
self.request = [[[SKProductsRequest alloc] initWithProductidentifiers:productIdentifiers] autorelease];
request.delegate = self;
[request start];
}
#program mark -
#program mark SKProductsRequestDelegate
- (void)prodcutsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
self.products = response.products;
SKProduct *product = [self.product objectAtIndex:0];
self.request = nil;
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
SKPayment
+ (id)paymentWithProductIdentifier:(NSString *)identifier //根据Product的Identifier购买,此方法已弃用
- (id)paymentWithProduct:(SKProduct *)product //先用 +paymentWithProduct获取可用的商品,用商品购买
SKPaymentQueue:
- addTransactionObserver:
- removeTransactionObserver:
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment]; //加入付款队列
#program mark -
#program mark SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
break;
case SKPaymentTransactionStateFailed:
break;
case SKPaymentTransactionStateRestored:
break;
default:
break;
}
}
}
IAP测试可能遇到的问题:
1.提示“You've already purchased this In-App Purchase but it hasn't been downloaded.”
交易结束后没有调用[SKPaymentQueue finishTransaction]将交易从支付队列中移出,不管支付成功还是失败都应调用此方法,校验成功或失败后也应该调用此方法。
2.获取不到product信息,数量为0
越狱的机器限制了IAP,针对越狱的机器应该使用第三方支付平台。