美文网首页
IAP-iOS内购流程

IAP-iOS内购流程

作者: 疯疯的小龙 | 来源:发表于2019-01-09 14:32 被阅读0次

目录

  1. 请求商品信息
  2. 添加/移除监听
  3. 购买
  4. 恢复购买

请求商品信息

  1. 发起请求
    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
    //保持对请求的强引用
    self.productsRequest = productsRequest;
    //SKProductsRequestDelegate
    productsRequest.delegate = self;
    [productsRequest start];
  1. SKProductsRequestDelegate请求商品信息回调
//收到商品反馈消息
- (void)productsRequest:(SKProductsRequest *)request
     didReceiveResponse:(SKProductsResponse *)response {
    self.products = response.products;
    for (NSString *invalidIdentifier in response.invalidProductIdentifiers) {
    // Handle any invalid product identifiers.
    
    }
}

//请求商品结束
- (void)requestDidFinish:(SKRequest *)request {

}

//请求商品信息失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

}

添加/移除监听

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
  • 添加监听后,当购买状态发生改变时系统会调用以下SKPaymentTransactionObserver的相应协议方法(实现监听方法)
@protocol SKPaymentTransactionObserver <NSObject>
@required
// Sent when the transaction array has changed (additions or state changes).  Client should check state of transactions and finish as appropriate.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions NS_AVAILABLE(10_7, 3_0);

@optional
// Sent when transactions are removed from the queue (via finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions NS_AVAILABLE(10_7, 3_0);

// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue.
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error NS_AVAILABLE(10_7, 3_0);

// Sent when all transactions from the user's purchase history have successfully been added back to the queue.
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue NS_AVAILABLE(10_7, 3_0);

// Sent when the download state has changed.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray<SKDownload *> *)downloads NS_AVAILABLE(10_8, 6_0);

// Sent when a user initiates an IAP buy from the App Store
- (BOOL)paymentQueue:(SKPaymentQueue *)queue shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)product NS_SWIFT_NAME(paymentQueue(_:shouldAddStorePayment:for:)) NS_AVAILABLE_IOS(11_0);

@end

  1. 实现监听方法
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
            {
                MTLOG(@"交易完成");
                  //校验交易是否合法 本地校验or自己的服务端校验
                  //1、记录订单信息,用于校验失败时下次启动app重新发起校验
                  //2、获取本地凭证  *必需
                  NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
                  NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
                  NSString *receiptStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

                  //若获取不到本地凭证,则调取刷新凭证的方法刷新凭证
                  SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];
                  request.delegate = self;
                  [request start];

                  //获取product_id *必需
                  NSString *product_id = transaction.payment.productIdentifier;

                  //获取transaction_id *必需
                  NSString * transaction_id = transaction.transactionIdentifier;

                  //3、根据本地凭证本地校验/服务端校验
                  //4、校验成功,更新数据库,刷新UI,移除记录的订单信息,结束订单
                  //(若不结束订单,苹果会一直调用 - (void)paymentQueue:updatedTransactions:直到结束该订单)
                  [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                  //5、校验失败,不移除记录的订单信息,不调用结束订单方法:[[SKPaymentQueue defaultQueue] finishTransaction:transaction]
                  //启动app时检查记录的订单信息数组,若非空说明有订单未成功校验,此时需重新调用校验方法
            }
                break;
            case SKPaymentTransactionStatePurchasing:
            {
                MTLOG(@"商品添加进列表");
            }
                break;
            case SKPaymentTransactionStateRestored:
            {
                MTLOG(@"恢复购买");
                //1、记录已购买过的商品,用于解锁商品。在恢复成功的回调里解锁商品
                //恢复成功回调:- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue;

            }
                break;
            case SKPaymentTransactionStateFailed:
            {
                MTLOG(@"交易失败");
            }
                break;
            default:
                break;
        }
    }
}

- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {

    MTLOG(@"移除购买队列");
}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
    
    MTLOG(@"恢复内购失败");
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    
    MTLOG(@"恢复内购完成");
    //1、恢复成功,解锁商品
}

  1. 获取本地凭证
  • 凭证用于校验订单是否合法
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
    NSString *receiptStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  • 若凭证不存在,则请求刷新凭证<SKRequestDelegate>
    SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];
    request.delegate = self;
    [request start];
  • <SKRequestDelegate> 刷新凭证回调,回调里重新校验订单
@protocol SKRequestDelegate <NSObject>

@optional
- (void)requestDidFinish:(SKRequest *)request NS_AVAILABLE(10_7, 3_0);
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error NS_AVAILABLE(10_7, 3_0);

@end

  • 根据检验返回的状态码判断购买是否有效/订阅是否过期
image.png
  • 校验成功,则调用finish方法结束订单。若不调用finish方法则苹果会一直调用- (void)paymentQueue:updatedTransactions:直到结束该订单
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

购买

  1. 判断是否能够购买
    BOOL canMakePayments = [SKPaymentQueue canMakePayments];
  1. 发起购买请求
  • 优先判断是否请求到商品信息,若未请求到则再次发起请求(详见请求商品信息
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    payment.applicationUsername = applicationUsername; //可以唯一标识用户账号即可,是用于apple检测非法活动
    [[SKPaymentQueue defaultQueue] addPayment:payment];
  1. 购买结果回调方法(详见添加/移除监听 — 1. 实现监听方法)
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;

恢复购买

  1. 发起恢复购买请求
    //恢复已购项目(只有非消耗性、免费(或限时免费)、自动订阅(有效期内)、免费订阅 的项目才能被恢复)
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  1. 恢复内购回调(详见添加/移除监听 — 1. 实现监听方法)
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;

相关文章

  • IAP-iOS内购流程

    目录 请求商品信息 添加/移除监听 购买 恢复购买 请求商品信息 发起请求 SKProductsRequestDe...

  • 苹果内购流程

    苹果内购流程

  • APP内购

    关于内购的介绍和如何添加内购就不在这介绍了。下面直接上内购流程以及实现 内购流程 1.向苹果请求准备卖的商品 2....

  • 内购流程

    一、提交资料 1.填写协议2.请求合同3.填写地址4.阅读并同意协议5.填写联系方式6.添加地址7.选择设置地址8...

  • 内购流程

    内购流程 1.构建内购的设置,需要设置内购商品,填写银行卡信息 进入苹果官方后台配置需要内购商品的信息(包括内购的...

  • 内购流程

    前提 这里默认,你已经有了 Apple 开发者账号,并且已经在 iTunes Connect -> 我的 App ...

  • 内购流程

    内购准备: 1.登陆开发者账号, 导入profile文件2.去Itunes Connect 添加应用信息, 内购信...

  • 内购流程

    内购完整详细的流程,网站如下: http://blog.sina.com.cn/s/blog_6f5fd9ba01...

  • 内购流程

    一、配置 二、支付 三、丢单处理

  • 苹果内购

    根据我自己的项目绘制了整个内购流程,当然一些后台的业务可以根据自己的业务需求处理。这个内购流程,是购买会员的内购流...

网友评论

      本文标题:IAP-iOS内购流程

      本文链接:https://www.haomeiwen.com/subject/aylurqtx.html