iOS內(nèi)購IAP(一) —— 基礎(chǔ)配置篇(一)

版本記錄

版本號 時間
V1.0 2017.05.03

前言

大家都知道ios上架apple store喇喉,app內(nèi)部的虛擬商品是需要走內(nèi)購的硅堆,和蘋果三七開,否則是上不了架的姓蜂,一般比如游戲里的金幣萎羔,寶石之類的都是需要走內(nèi)購的液走,下面說一下內(nèi)購的流程,下面是原文地址 —— app內(nèi)購,謝謝分享缘眶。

詳細(xì)流程

一嘱根、協(xié)議的填寫

直接上圖了。

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

CNAPS CODE 查詢地址

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

二巷懈、創(chuàng)建內(nèi)購項目

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

三该抒、添加內(nèi)購項目測試賬號

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

四、代碼實現(xiàn)

//首先導(dǎo)入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"

//在內(nèi)購項目中創(chuàng)的商品單號
#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(@"允許程序內(nèi)付費購買");
   }
    else {
         NSLog(@"不允許程序內(nèi)付費購買");
         UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:@"提示"
         message:@"您的手機沒有打開程序內(nèi)付費購買"
    delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];

         [alerView show];
     }
}

- (void)RequestProductData
{
    NSLog(@"---------請求對應(yīng)的產(chǎn)品信息------------");
    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> 請求協(xié)議
//收到的產(chǎn)品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

    NSLog(@"-----------收到產(chǎn)品反饋信息--------------");
    NSArray *myProduct = response.products;
    NSLog(@"產(chǎn)品Product ID:%@",response.invalidProductIdentifiers);
    NSLog(@"產(chǎn)品付費數(shù)量: %d", (int)[myProduct count]);
    // populate UI
    for(SKProduct *product in myProduct){
    NSLog(@"product info");
    NSLog(@"SKProduct 描述信息%@", [product description]);
    NSLog(@"產(chǎn)品標(biāo)題 %@" , product.localizedTitle);
    NSLog(@"產(chǎn)品描述信息: %@" , 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(@"---------發(fā)送購買請求------------");
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

- (void)requestProUpgradeProductData
{
    NSLog(@"------請求升級數(shù)據(jù)---------");
    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(@"----------反饋信息結(jié)束--------------");

}

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

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

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結(jié)果
{
    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(@"關(guān)閉",nil) otherButtonTitles:nil];

            [alerView show];

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

            [alerView2 show];

        }break;
        case SKPaymentTransactionStateRestored://已經(jīng)購買過該商品
            [self restoreTransaction:transaction];
            NSLog(@"-----已經(jīng)購買過該商品 --------");
            case SKPaymentTransactionStatePurchasing:      //商品添加進(jìn)列表
            NSLog(@"-----商品添加進(jìn)列表 --------");
            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(@"-----記錄交易--------");
}

//處理下載內(nèi)容
- (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(@" 交易恢復(fù)處理");
}

- (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];//解除監(jiān)聽
}

@end

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

后記

再次謝謝技術(shù)達(dá)人的分享恳谎,希望每一個熱愛技術(shù)的工程師都可以從中受益芝此,共同成長。未完因痛,待續(xù)~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末婚苹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鸵膏,更是在濱河造成了極大的恐慌膊升,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件较性,死亡現(xiàn)場離奇詭異用僧,居然都是意外死亡结胀,警方通過查閱死者的電腦和手機赞咙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來糟港,“玉大人攀操,你說我怎么就攤上這事〗崭В” “怎么了速和?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長剥汤。 經(jīng)常有香客問我颠放,道長,這世上最難降的妖魔是什么吭敢? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任碰凶,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘欲低。我一直安慰自己辕宏,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布砾莱。 她就那樣靜靜地躺著瑞筐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪腊瑟。 梳的紋絲不亂的頭發(fā)上聚假,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天,我揣著相機與錄音扫步,去河邊找鬼魔策。 笑死,一個胖子當(dāng)著我的面吹牛河胎,可吹牛的內(nèi)容都是我干的闯袒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼游岳,長吁一口氣:“原來是場噩夢啊……” “哼政敢!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起胚迫,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤喷户,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后访锻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體褪尝,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年期犬,在試婚紗的時候發(fā)現(xiàn)自己被綠了河哑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡龟虎,死狀恐怖璃谨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鲤妥,我是刑警寧澤佳吞,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站棉安,受9級特大地震影響底扳,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贡耽,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一衷模、第九天 我趴在偏房一處隱蔽的房頂上張望羡滑。 院中可真熱鬧,春花似錦算芯、人聲如沸柒昏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽职祷。三九已至,卻和暖如春届囚,著一層夾襖步出監(jiān)牢的瞬間有梆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工意系, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留泥耀,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓蛔添,卻偏偏與公主長得像痰催,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子迎瞧,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容