內(nèi)購(gòu)的前提:
有個(gè)人開(kāi)發(fā)者賬號(hào);開(kāi)通了銀行業(yè)務(wù);在開(kāi)發(fā)者后臺(tái)添加了內(nèi)購(gòu)產(chǎn)品;創(chuàng)建了相應(yīng)的Bundle id 和證書(shū);
第一,添加StoreKit.framework 框架;圖1
第二,配置相應(yīng)的Bundile Indentfier 和證書(shū);圖2
第三,#import <StoreKit/StoreKit.h>,添加協(xié)議SKPaymentTransactionObserver,SKProductsRequestDelegate
根據(jù)內(nèi)購(gòu)產(chǎn)品的id查詢可以購(gòu)買(mǎi)的產(chǎn)品:
NSSet * nsset = [NSSet setWithArray:arrProduct];
SKProductsRequest * request = [[SKProductsRequest alloc]initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
協(xié)議回調(diào):
#pragma mark -- SKProductsRequestDelegate delegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSArray * product = response.products;
if (product.count == 0) {//沒(méi)有購(gòu)買(mǎi)內(nèi)購(gòu)產(chǎn)品
[self.payDelegate onGetFail:non_Products];
}else{//返回內(nèi)購(gòu)產(chǎn)品
[self.payDelegate onGetSuccess:product];
}
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error{
[self.payDelegate onGetFail:fail_Products];//獲取有錯(cuò)
}
-(void)requestDidFinish:(SKRequest *)request{
//請(qǐng)求完成
}
第四,購(gòu)買(mǎi)產(chǎn)品,product是 SKProduct 類(lèi)型
-(void)getAppStorePay:(id)product{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
購(gòu)買(mǎi)成功或者失敗的回調(diào):
#pragma mark -- SKPaymentTransactionObserver delegate//監(jiān)聽(tīng)購(gòu)買(mǎi)結(jié)果-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray*)transactions{
for(SKPaymentTransaction * tran in transactions){
//? ? ? ? ? ? NSLog(@"購(gòu)買(mǎi)狀態(tài)%ld",(long)tran.transactionState);
if (tran.transactionState == SKPaymentTransactionStatePurchased) {
//? ? ? ? ? ? NSLog(@"購(gòu)買(mǎi)成功");
NSURL * receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
// 從沙盒中獲取到購(gòu)買(mǎi)憑據(jù)
NSData * receiptData = [NSData dataWithContentsOfURL:receiptURL];
NSString * encodeStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
[self.payDelegate onAppStorePaySuccess:encodeStr];
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}else if (tran.transactionState == SKPaymentTransactionStateRestored){
//? ? ? ? ? ? NSLog(@"重新購(gòu)買(mǎi)");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}else if (tran.transactionState == SKPaymentTransactionStateFailed){
//? ? ? ? ? ? NSLog(@"購(gòu)買(mǎi)失敗");
[self.payDelegate onAppStorePayFail];
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
else if (tran.transactionState == SKPaymentTransactionStatePurchasing){
//? ? ? ? ? ? NSLog(@"正在購(gòu)買(mǎi)");
}else{
//? ? ? ? ? ? NSLog(@"排隊(duì)中");
}
}
}
//交易結(jié)束
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
//? ? NSLog(@"交易完成");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)dealloc{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
獲取到的購(gòu)買(mǎi)憑證一般是放在后臺(tái)進(jìn)行驗(yàn)證:
#define ITMS_PROD_VERIFY_RECEIPT_URL? ? ? ? @"https://buy.itunes.apple.com/verifyReceipt"http://驗(yàn)證上架的
#define ITMS_SANDBOX_VERIFY_RECEIPT_URL? ? @"https://sandbox.itunes.apple.com/verifyReceipt"http://驗(yàn)證沙盒測(cè)試的
前端實(shí)例:
/*
驗(yàn)證購(gòu)買(mǎi)憑證一般都是在服務(wù)端進(jìn)行驗(yàn)證
*/
// 發(fā)送網(wǎng)絡(luò)POST請(qǐng)求哆料,對(duì)購(gòu)買(mǎi)憑據(jù)進(jìn)行驗(yàn)證
NSURL *url = [NSURL URLWithString:ITMS_SANDBOX_VERIFY_RECEIPT_URL];
// 國(guó)內(nèi)訪問(wèn)蘋(píng)果服務(wù)器比較慢罕袋,timeoutInterval需要長(zhǎng)一點(diǎn)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
request.HTTPMethod = @"POST";
NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", receiptData];
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = payloadData;
// 提交驗(yàn)證請(qǐng)求蔬啡,并獲得官方的驗(yàn)證JSON結(jié)果
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 官方驗(yàn)證結(jié)果為空
if (result == nil) {
NSLog(@"驗(yàn)證失敗");
}
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingAllowFragments error:nil];
if (dict != nil) {
NSLog(@"驗(yàn)證成功");
}