如果你的開發者賬號中未進行過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,針對越獄的機器應該使用第三方支付平台。