IAP-iOS内购流程

转载自: jianshu.com/p/74a0866bd8d7

目录

  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

  • 根据检验返回的状态码判断购买是否有效/订阅是否过期

webpuploading.4e448015.gif转存失败重新上传取消

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



作者:疯疯的小龙
链接:https://www.jianshu.com/p/74a0866bd8d7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值