objective-c - 已在 StoreKit 中购买订阅

标签 objective-c ios storekit

我在 iOS 应用程序的应用程序购买中使用可续订订阅。 当用户尝试购买他们已经支付的订阅时,iTunes 会显示一条消息“您当前已订阅此”。

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

在 paymentQueue:updatedTransactions: 观察者的方法中,它作为 SKPaymentTransactionStateFailed 通过。如何区分此类故障和其他故障(例如用户按下取消按钮)?

我是提交返回的交易还是需要调用 restorePreviousTransactions。

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

最佳答案

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(订阅已过期),或者其他(我认为 0 和 21006 以外的任何内容都是实际错误)。

我做事的方式是将交易详细信息存储在一个 PLIST 文件中,该文件存储在文档目录中。

您可以向 PLIST 添加额外的字段,例如到期日期、 bool 标志等。

通过这种方式您可以获得收据的副本,但您应该始终验证它,因为它可能已经过期。

Q: In the paymentQueue:updatedTransactions: method of the observer it is coming through as a SKPaymentTransactionStateFailed. How do I distinguish between this type of failure and other failures such as the user pressing the cancel buttons?

您在 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 处理失败,但我没有为取消编写代码,因为我没有理由在我的应用程序中这样做。

Q: Do I submit the transaction that is returned or do I need to call restorePreviousTransactions.

我相信 StoreKit 在内部使用 finishTransaction 方法和 restorePreviousTransaction 方法处理这个问题

即,

[[SKPaymentQueue defaultQueue] finishTransaction: 交易];

完成交易

希望对你有帮助

关于objective-c - 已在 StoreKit 中购买订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177940/

相关文章:

ios - objective-c 完成 block 问题

ios - 使用 Parse 作为后端在 Xcode 6 中使用 Swift 创建注册

swift - 检查用户是否已经在 SwiftyStoreKit 中购买了产品

ios - 如何检查沙箱服务器状态? ("Cannot connect to itunes store")

iphone - UITableView 部分索引与搜索栏重叠

Objective-C 类方法 : dot syntax and 'class property'

ios - Swift 中文本字段或 TextView 中的多个关键字

ios - 使用 SKPaymentTransactionObserver 打开 View 后,应用程序不断询问 Apple ID

ios - 如何在对象中声明数组并使用编码存储

javascript - 如何在 iOS 上的 Safari 中滚动时监视滚动位置?