iOS内购IAP(一) —— 基础配置篇(一)

版本记录

版本号 时间
V1.0 2017.05.03

前言

大家都知道ios上架apple store,app内部的虚拟商品是需要走内购的,和苹果三七开,否则是上不了架的,一般比如游戏里的金币,宝石之类的都是需要走内购的,下面说一下内购的流程,下面是原文地址 —— app内购,谢谢分享。

详细流程

一、协议的填写

直接上图了。

iOS内购IAP(一) —— 基础配置篇(一)_第1张图片
第1步
iOS内购IAP(一) —— 基础配置篇(一)_第2张图片
第2步
iOS内购IAP(一) —— 基础配置篇(一)_第3张图片
第3步
iOS内购IAP(一) —— 基础配置篇(一)_第4张图片
第4步
iOS内购IAP(一) —— 基础配置篇(一)_第5张图片
第5步
iOS内购IAP(一) —— 基础配置篇(一)_第6张图片
第6步
iOS内购IAP(一) —— 基础配置篇(一)_第7张图片
第7步
iOS内购IAP(一) —— 基础配置篇(一)_第8张图片
第8步
iOS内购IAP(一) —— 基础配置篇(一)_第9张图片
第9步
iOS内购IAP(一) —— 基础配置篇(一)_第10张图片
第10步

CNAPS CODE 查询地址

iOS内购IAP(一) —— 基础配置篇(一)_第11张图片
第11步
iOS内购IAP(一) —— 基础配置篇(一)_第12张图片
第12步
iOS内购IAP(一) —— 基础配置篇(一)_第13张图片
第13步
iOS内购IAP(一) —— 基础配置篇(一)_第14张图片
第14步
iOS内购IAP(一) —— 基础配置篇(一)_第15张图片
第15步
iOS内购IAP(一) —— 基础配置篇(一)_第16张图片
第16步
iOS内购IAP(一) —— 基础配置篇(一)_第17张图片
第17步
iOS内购IAP(一) —— 基础配置篇(一)_第18张图片
第18步
iOS内购IAP(一) —— 基础配置篇(一)_第19张图片
第19步
iOS内购IAP(一) —— 基础配置篇(一)_第20张图片
第20步
iOS内购IAP(一) —— 基础配置篇(一)_第21张图片
第21步

二、创建内购项目

iOS内购IAP(一) —— 基础配置篇(一)_第22张图片
第1步
iOS内购IAP(一) —— 基础配置篇(一)_第23张图片
第2步
iOS内购IAP(一) —— 基础配置篇(一)_第24张图片
第3步
iOS内购IAP(一) —— 基础配置篇(一)_第25张图片
第4步
iOS内购IAP(一) —— 基础配置篇(一)_第26张图片
第5步
iOS内购IAP(一) —— 基础配置篇(一)_第27张图片
第6步
iOS内购IAP(一) —— 基础配置篇(一)_第28张图片
第7步

三、添加内购项目测试账号

iOS内购IAP(一) —— 基础配置篇(一)_第29张图片
第1步:创建测试账号
第2步:添加沙盒测试员
iOS内购IAP(一) —— 基础配置篇(一)_第30张图片
第3步:填写账号信息

四、代码实现

//首先导入StoreKit.framework库
1 .h文件
#import 

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

//代理
@interface RechargeVC : UIViewController 

{
    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"//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];
}

// 请求协议
//收到的产品信息
- (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];
}

// 千万不要忘记绑定,代码如下:
//----监听购买结果
//[[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提交审核内购项目自然跟着改变状态,不消耗物品请不要选择 托管那一项,不然会出现问题。

后记

再次谢谢技术达人的分享,希望每一个热爱技术的工程师都可以从中受益,共同成长。未完,待续~~~~

iOS内购IAP(一) —— 基础配置篇(一)_第31张图片

你可能感兴趣的:(iOS内购IAP(一) —— 基础配置篇(一))