//需要遵守的代理
<SKStoreProductViewControllerDelegate,UIGestureRecognizerDelegate,SKPaymentTransactionObserver,SKProductsRequestDelegate>
#import <StoreKit/StoreKit.h>
//沙盒測試環(huán)境驗(yàn)證
#define SANDBOX @"https://sandbox.itunes.apple.com/verifyReceipt"
//正式環(huán)境驗(yàn)證
#define AppStore @"https://buy.itunes.apple.com/verifyReceipt"
- (void)buyBtnAction {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
NSArray* transactions = [SKPaymentQueue defaultQueue].transactions;
for (SKPaymentTransaction *transaction in transactions) {
NSLog(@"transactionState:%ld", transaction.transactionState);
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
if([SKPaymentQueue canMakePayments]){
NSLog(@"-------------請(qǐng)求對(duì)應(yīng)的產(chǎn)品信息----------------");
NSArray *product = [[NSArray alloc] initWithObjects:self.product_id,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}else{
NSLog(@"不允許程序內(nèi)付費(fèi)");
}
}
//收到產(chǎn)品返回信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"--------------收到產(chǎn)品反饋消息---------------------");
NSArray *product = response.products;
if([product count] == 0){
NSLog(@"--------------沒有商品------------------");
return;
}
NSLog(@"productID:%@", response.invalidProductIdentifiers);
NSLog(@"產(chǎn)品付費(fèi)數(shù)量:%lu",(unsigned long)[product count]);
SKProduct *p = nil;
for (SKProduct *pro in product) {
NSLog(@"%@", [pro description]);
NSLog(@"%@", [pro localizedTitle]);
NSLog(@"%@", [pro localizedDescription]);
NSLog(@"%@", [pro price]);
NSLog(@"%@", [pro productIdentifier]);
if([pro.productIdentifier isEqualToString:self.product_id]){
p = pro;
}
}
SKPayment *payment = [SKPayment paymentWithProduct:p];
NSLog(@"發(fā)送購買請(qǐng)求");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
//請(qǐng)求失敗
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"------------------錯(cuò)誤-----------------:%@", error);
}
- (void)requestDidFinish:(SKRequest *)request{
NSLog(@"------------反饋信息結(jié)束-----------------");
}
/**
* 驗(yàn)證購買,避免越獄軟件模擬蘋果請(qǐng)求達(dá)到非法購買問題
*
*/
-(void)verifyPurchaseWithPaymentTransaction{
//從沙盒中獲取交易憑證并且拼接成請(qǐng)求體數(shù)據(jù)
NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl];
NSString *receiptString=[receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];//轉(zhuǎn)化為base64字符串
NSString *bodyString = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", receiptString];//拼接請(qǐng)求數(shù)據(jù)
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
//創(chuàng)建請(qǐng)求到蘋果官方進(jìn)行購買驗(yàn)證
NSURL *url = [NSURL URLWithString:AppStore];
NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:url];
requestM.HTTPBody = bodyData;
requestM.HTTPMethod = @"POST";
//創(chuàng)建連接并發(fā)送同步請(qǐng)求
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:requestM returningResponse:nil error:&error];
if (error) {
NSLog(@"Appstore驗(yàn)證購買過程中發(fā)生錯(cuò)誤,錯(cuò)誤信息:%@",error.localizedDescription);
return;
}
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
if([dic[@"status"] intValue] == 0){
NSLog(@"Appstore下購買成功!");
[self rechargeSuccess];
NSArray *items = @[MMItemMake(@"確定", MMItemTypeHighlight, nil)];
[[[MMAlertView alloc] initWithTitle:@"充值成功!" detail:nil items:items] showWithBlock:nil];
}else{
NSLog(@"Appstore下購買失敗,未通過驗(yàn)證!");
//創(chuàng)建請(qǐng)求到蘋果官方進(jìn)行購買驗(yàn)證
NSURL *url2 = [NSURL URLWithString:SANDBOX];
NSMutableURLRequest *requestM2 = [NSMutableURLRequest requestWithURL:url2];
requestM2.HTTPBody = bodyData;
requestM2.HTTPMethod = @"POST";
//創(chuàng)建連接并發(fā)送同步請(qǐng)求
NSError *error2 = nil;
NSData *responseData2 = [NSURLConnection sendSynchronousRequest:requestM2 returningResponse:nil error:&error2];
if (error2) {
NSLog(@"SandBox驗(yàn)證購買過程中發(fā)生錯(cuò)誤,錯(cuò)誤信息:%@",error2.localizedDescription);
return;
}
NSDictionary *dic2 = [NSJSONSerialization JSONObjectWithData:responseData2 options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic2);
if([dic2[@"status"] intValue] == 0){
NSLog(@"SandBox下購買成功!");
[self rechargeSuccess];
NSArray *items = @[MMItemMake(@"確定", MMItemTypeHighlight, nil)];
[[[MMAlertView alloc] initWithTitle:@"充值成功!" detail:nil items:items] showWithBlock:nil];
}else{
NSLog(@"SandBox下購買失敗,未通過驗(yàn)證!");
}
}
}
//監(jiān)聽購買結(jié)果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{
for(SKPaymentTransaction *tran in transaction){
switch (tran.transactionState) {
case SKPaymentTransactionStatePurchased:{
NSLog(@"交易完成");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
[self verifyPurchaseWithPaymentTransaction];
}
break;
case SKPaymentTransactionStatePurchasing:
NSLog(@"商品添加進(jìn)列表");
break;
case SKPaymentTransactionStateRestored:{
NSLog(@"已經(jīng)購買過商品");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
case SKPaymentTransactionStateFailed:{
NSLog(@"交易失敗");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
default:
break;
}
}
}
//交易結(jié)束
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
NSLog(@"交易結(jié)束");
}
- (void)dealloc {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
iOS 內(nèi)購充值代碼
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 最新更新:新增了選中項(xiàng)(默認(rèn)選中第一個(gè)) 前言:項(xiàng)目中用到了內(nèi)購,這里主要想記錄一下內(nèi)購代碼封裝和充值界面的封裝,...
- 內(nèi)購: --應(yīng)用程序本身的增值產(chǎn)品,游戲裝備,應(yīng)用程序中增值功能同樣可以內(nèi)購 --三(蘋果)七(開發(fā)商)開 --內(nèi)...
- 繼上一篇iOS內(nèi)購圖文教程,下面是代碼教程創(chuàng)建一個(gè)單例類.h文件 .m文件 在禮物頁面,調(diào)起單例,添加內(nèi)購代理方法...
- 夏日!天藍(lán),清澈的藍(lán),云白,純凈的白。 藍(lán)如蘭花,白如冰雪。 夏熱,寧靜,微微蟬鳴,吵鬧,沙沙樹擺 靜如鐘聲,鬧如魚躍