iOS內購IAP(一) —— 基礎配置篇(一)

版本記錄

版本號 時間
V1.0 2017.05.03

前言

大家都知道ios上架apple store,app內部的虛擬商品是需要走內購的,和蘋果三七開,否則是上不了架的,一般比如游戲里的金幣,寶石之類的都是需要走內購的,下面說一下內購的流程,下面是原文地址 —— app內購,謝謝分享。

詳細流程

一、協議的填寫

直接上圖了。

第1步
第2步
第3步
第4步
第5步
第6步
第7步
第8步
第9步
第10步

CNAPS CODE 查詢地址

第11步
第12步
第13步
第14步
第15步
第16步
第17步
第18步
第19步
第20步
第21步

二、創建內購項目

第1步
第2步
第3步
第4步
第5步
第6步
第7步

三、添加內購項目測試賬號

第1步:創建測試賬號
第2步:添加沙盒測試員
第3步:填寫賬號信息

四、代碼實現

//首先導入StoreKit.framework庫
1 .h文件
#import <StoreKit/StoreKit.h>

enum{
      IAP0p20=20,
      IAP1p100,
      IAP4p600,
      IAP9p1000,
      IAP24p6000,
}  buyCoinsTag;

//代理
@interface RechargeVC : UIViewController <SKPaymentTransactionObserver,SKProductsRequestDelegate >

{
    int buyType;
}

- (void) requestProUpgradeProductData;

- (void)RequestProductData;

- (void)buy:(int)type;

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;

- (void) PurchasedTransaction: (SKPaymentTransaction *)transaction;

- (void) completeTransaction: (SKPaymentTransaction *)transaction;

- (void) failedTransaction: (SKPaymentTransaction *)transaction;

- (void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;

- (void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;

- (void) restoreTransaction: (SKPaymentTransaction *)transaction;

- (void)provideContent:(NSString *)product;

- (void)recordTransaction:(NSString *)product;

@end

2  .m文件

#import "RechargeVC.h"

//在內購項目中創的商品單號
#define ProductID_IAP0p20 @"Nada.JPYF01"http://20
#define ProductID_IAP1p100 @"Nada.JPYF02" //100
#define ProductID_IAP4p600 @"Nada.JPYF03" //600
#define ProductID_IAP9p1000 @"Nada.JPYF04" //1000
#define ProductID_IAP24p6000 @"Nada.JPYF05" //6000

@interface RechargeVC ()

@end

@implementation RechargeVC

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [self buy:IAP0p20];

}

- (void)buy:(int)type
{
    buyType = type;
    if ([SKPaymentQueue canMakePayments]) {
    [self RequestProductData];
    NSLog(@"允許程序內付費購買");
   }
    else {
         NSLog(@"不允許程序內付費購買");
         UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"提示"
         message:@"您的手機沒有打開程序內付費購買"
    delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];

         [alerView show];
     }
}

- (void)RequestProductData
{
    NSLog(@"---------請求對應的產品信息------------");
    NSArray *product = nil;
    switch (buyType) {
        case IAP0p20:
          product=[[NSArray alloc] initWithObjects:ProductID_IAP0p20,nil];
          break;
        case IAP1p100:
          product=[[NSArray alloc] initWithObjects:ProductID_IAP1p100,nil];
          break;
        case IAP4p600:
          product=[[NSArray alloc] initWithObjects:ProductID_IAP4p600,nil];
          break;
        case IAP9p1000:
          product=[[NSArray alloc] initWithObjects:ProductID_IAP9p1000,nil];
          break;
        case IAP24p6000:
          product=[[NSArray alloc] initWithObjects:ProductID_IAP24p6000,nil];
          break;

        default:
           break;
}
    NSSet *nsset = [NSSet setWithArray:product];
    SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
    request.delegate=self;
    [request start];
}

//<SKProductsRequestDelegate> 請求協議
//收到的產品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

    NSLog(@"-----------收到產品反饋信息--------------");
    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(@"SKProduct 描述信息%@", [product description]);
    NSLog(@"產品標題 %@" , product.localizedTitle);
    NSLog(@"產品描述信息: %@" , product.localizedDescription);
    NSLog(@"價格: %@" , product.price);
    NSLog(@"Product id: %@" , product.productIdentifier);
}
    SKPayment *payment = nil;
    switch (buyType) {
        case IAP0p20:
          payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP0p20];    //支付25
          break;
        case IAP1p100:
          payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP1p100];    //支付108
          break;
        case IAP4p600:
          payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP4p600];    //支付618
          break;
        case IAP9p1000:
          payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP9p1000];    //支付1048
          break;
        case IAP24p6000:
          payment  = [SKPayment paymentWithProductIdentifier:ProductID_IAP24p6000];    //支付5898
          break;
        default:
          break;
}
    NSLog(@"---------發送購買請求------------");
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

- (void)requestProUpgradeProductData
{
    NSLog(@"------請求升級數據---------");
    NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
    SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];

}

//彈出錯誤信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
    NSLog(@"-------彈出錯誤信息----------");
    UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
    delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
    [alerView show];

}

- (void) requestDidFinish:(SKRequest *)request
{
    NSLog(@"----------反饋信息結束--------------");

}

- (void) PurchasedTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@"-----PurchasedTransaction----");
    NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
    [self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
}

//<SKPaymentTransactionObserver> 千萬不要忘記綁定,代碼如下:
//----監聽購買結果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結果
{
    NSLog(@"-----paymentQueue--------");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:{//交易完成
            [self completeTransaction:transaction];
            NSLog(@"-----交易完成 --------");

            UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@""
            message:@"購買成功"
            delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];

            [alerView show];

        } break;
        case SKPaymentTransactionStateFailed://交易失敗
            { [self failedTransaction:transaction];
            NSLog(@"-----交易失敗 --------");
            UIAlertView *alerView2 =  [[UIAlertView alloc] initWithTitle:@"提示"
            message:@"購買失敗,請重新嘗試購買"
            delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];

            [alerView2 show];

        }break;
        case SKPaymentTransactionStateRestored://已經購買過該商品
            [self restoreTransaction:transaction];
            NSLog(@"-----已經購買過該商品 --------");
            case SKPaymentTransactionStatePurchasing:      //商品添加進列表
            NSLog(@"-----商品添加進列表 --------");
            break;
            default:
            break;
        }
    }
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction

{
    NSLog(@"-----completeTransaction--------");
    // Your application should implement these two methods.
    NSString *product = transaction.payment.productIdentifier;
    if ([product length] > 0) {

    NSArray *tt = [product componentsSeparatedByString:@"."];
    NSString *bookid = [tt lastObject];
    if ([bookid length] > 0) {
    [self recordTransaction:bookid];
    [self provideContent:bookid];
    }
}

    // Remove the transaction from the payment queue.

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

//記錄交易
- (void)recordTransaction:(NSString *)product
{
    NSLog(@"-----記錄交易--------");
}

//處理下載內容
- (void)provideContent:(NSString *)product
{
    NSLog(@"-----下載--------");
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction{
    NSLog(@"失敗");
    if (transaction.error.code != SKErrorPaymentCancelled)
    {

    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

- (void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction
{

}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    NSLog(@" 交易恢復處理");
}

- (void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
    NSLog(@"-------paymentQueue----");
}

#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"%@",  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    switch([(NSHTTPURLResponse *)response statusCode]) 
{
        case 200:
        case 206:
              break;
        case 304:
              break;
        case 400:
              break;
        case 404:
              break;
        case 416:
              break;
        case 403:
              break;
        case 401:
        case 500:
              break;
        default:
              break;
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    NSLog(@"test");
}

- (void)dealloc
{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除監聽
}

@end

需要說明的是:內購項目不再是單獨提交審核了,只要app提交審核內購項目自然跟著改變狀態,不消耗物品請不要選擇 托管那一項,不然會出現問題。

后記

再次謝謝技術達人的分享,希望每一個熱愛技術的工程師都可以從中受益,共同成長。未完,待續~~~~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,119評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,382評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,038評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,853評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,616評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,112評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,192評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,355評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,869評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,727評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,928評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,467評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,165評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,570評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,813評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,585評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,892評論 2 372

推薦閱讀更多精彩內容