首页 文章

已经在StoreKit中购买了订阅

提问于
浏览
7

我在iOS应用程序中的应用程序购买中使用可再生订阅 . 当用户尝试购买已经为消息付款的订阅时,将显示iTunes“您当前已订阅此消息” .

我如何检测此事件何时发生,以便我可以处理事务并授予对我的应用程序的访问权限 .

在paymentQueue:updatedTransactions:它作为SKPaymentTransactionStateFailed传递的观察者的方法 . 如何区分此类故障和其他故障,例如用户按下取消按钮?

我是否提交了返回的事务,或者我是否需要调用restorePreviousTransactions .

在Apple文档中,它声明“如果用户尝试购买他们已经购买的非消费品或可更新订阅,您的应用程序将收到该项目的常规交易,而不是恢复交易 . 但是,用户不会再次收费对于该产品,您的应用程序应将这些交易与原始交易的交易完全相同 . “

1 回答

  • 1
    Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app.
    

    您通过Apple验证(我使用php网站代码执行此操作)检测订阅何时存在,您将获得“状态代码”响应,并且可以验证它是否为代码21006(订阅已过期)或其他(I将0和21006以外的任何内容视为实际错误) .

    我做事的方式是将事务详细信息存储在我存储在文档目录中的PLIST文件中 .

    您可以向PLIST添加额外的字段,例如expiryDates,boolean flags等 .

    这样您就可以获得收据的副本,但您应该始终对其进行验证,因为它可能已过期 .

    问:在paymentQueue:updatedTransactions:它作为SKPaymentTransactionStateFailed传递的观察者的方法 . 如何区分此类故障和其他故障,例如用户按下取消按钮?

    您可以在updatedTransactions方法中使用switch语句来确定不同类型的响应 .

    -(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
    {    
        NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription];
        NSLog(@"Error - %@", message);
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                            message:message 
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
    
    -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
    {
        NSLog(@"updatedTransactions");
        for (SKPaymentTransaction *transaction in transactions)
        {
            switch (transaction.transactionState)
            {
                case SKPaymentTransactionStatePurchasing:
                    // take action whilst processing payment
                    break;
    
                case SKPaymentTransactionStatePurchased:
                    // take action when feature is purchased
                    break;
    
                case SKPaymentTransactionStateRestored:
                    // take action to restore the app as if it was purchased            
                    break;
    
    
                case SKPaymentTransactionStateFailed:
                    if (transaction.error.code != SKErrorPaymentCancelled)
                    {
                    // Do something with the error
                    } // end if
                    break;
    
                default:
                    break;
            } // end switch
    
        } // next
    

    TransactionStateFailed处理失败,虽然我不编码取消,因为我没有理由在我的应用程序中这样做 .

    问:我是否提交了退回的交易,或者我是否需要调用restorePreviousTransactions .

    我相信StoreKit使用finishTransaction方法和restorePreviousTransaction方法在内部处理它

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

    完成交易

    我希望这有帮助

相关问题