已经在StoreKit中购买了订阅 [英] Already purchased subscriptions in StoreKit

查看:99
本文介绍了已经在StoreKit中购买了订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS应用中的应用购买中使用了可再生订阅. 当用户尝试购买他们已经为消息付费的订阅时,会从iTunes中显示您当前已订阅此消息".

I am using renewable subscriptions in app purchases in an iOS app. When a user attempts to purchase a subscription that they already have paid for a message is displayed from iTunes "You're currently subscribed to this".

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

How I can detect when this event has occurred so that I can process the transaction and grant access to my app.

在观察者的paymentQueue:updatedTransactions:方法中,以SKPaymentTransactionStateFailed的形式通过.如何区分这种类型的故障和其他故障,例如用户按下取消"按钮?

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?

我是否提交返回的交易,还是需要致电restorePreviousTransactions.

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

在Apple文档中指出:如果用户尝试购买他们已经购买的非消耗性产品或可再生订阅,则您的应用程序会收到该商品的常规交易,而不是还原交易.但是,用户不会对该产品再次收费.您的应用程序应将这些交易与原始交易相同."

In the Apple documents it states "If the user attempts to purchase a non-consumable product or a renewable subscription they have already purchased, your application receives a regular transaction for that item, not a restore transaction. However, the user is not charged again for that product. Your application should treat these transactions identically to those of the original transaction."

推荐答案

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之外的其他都是实际错误).

You detect when the subscription exists via validation with Apple (I use php website code to do this), you get a "status code" response back, and can verify whether it is a code 21006 (subscription is expired), or others (I consider anything other than 0 and 21006 to be an actual error).

我做事的方式是将交易明细存储在我存储在documents目录中的PLIST文件中.

The way I do things is I store the transaction details inside a PLIST file which I store in the documents directory.

您可以在PLIST中添加其他字段,例如expiryDates,布尔标志等.

You could add extra fields to the PLIST such as expiryDates, boolean flags, etc.

通过这种方式,您可以获得收据的副本,尽管您应该始终对其进行验证,因为它可能已过期.

This way you have a copy of the receipt, although you should always validate it because it might have expired.

问:在观察者的paymentQueue:updatedTransactions:方法中 正以SKPaymentTransactionStateFailed的形式出现.我如何能 区分这种类型的故障和其他故障,例如 用户按下取消按钮?

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语句来确定不同类型的响应.

You use a switch statement in the updatedTransactions method to determine the different types of responses.

示例

-(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处理失败,尽管我没有为取消编写代码,因为我没有理由在我的应用中这样做.

The TransactionStateFailed handles the failures, although I do not code for cancellation as there is no reason for me to do so in my app.

问:我要提交返回的交易还是需要致电 restorePreviousTransactions.

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

我相信StoreKit可以使用finishTransaction方法和restorePreviousTransaction方法在内部进行处理

I believe StoreKit handles this internally with finishTransaction method and restorePreviousTransaction methods

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

完成交易

我希望这对您有帮助

这篇关于已经在StoreKit中购买了订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆