【问题标题】:Failing a restore of a transaction when a transaction doesn't exist with in app purchases and objective c当应用内购买和目标 c 不存在交易时无法恢复交易
【发布时间】:2015-06-28 16:00:54
【问题描述】:

我有一个正在开发应用内购买的应用。整个购买过程正在运行,当使用 Apple ID 进行有效购买时,恢复也正在运行。但是,当用户点击“恢复购买”UIButton 时,如果没有使用登录的 Apple 帐户进行购买活动,我想处理这种情况。

在我的AppDelegate 中,我有:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{    
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
            {
                // unlock the full version
                [self unlockFullVersion:transaction];
                [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                [self saveReceipts];
                break;
            }

            case SKPaymentTransactionStatePurchasing:
            {
                // currently purchasing
                break;
            }
            case SKPaymentTransactionStateRestored:
            {
                [self restoreTransaction:transaction]; 
                [[SKPaymentQueue defaultQueue]finishTransaction:transaction];

                break;
            }
            case SKPaymentTransactionStateFailed:
            {
                // it didn't work
                break;
            }
            case SKPaymentTransactionStateDeferred:
            {
                // deferred
                break;
            }
        }
    }

}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
    if (transaction)
    {

        if ([transaction.payment.productIdentifier isEqualToString:@"com.company.Unlimited"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CheckProVersion" object:self userInfo:nil];
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"IAPSuccessful"];


            [[NSUserDefaults standardUserDefaults]synchronize];
        }

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Restore Successful" message:@"Your restore was successful." delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil, nil];
        [alertView show];
    }
    else
    {
        NSLog(@"The transaction does not exist");

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Restore Unsuccessful" message:@"Your Apple ID doesn't have an active purchase for upgrading to Envylope Pro. " delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

在这种特殊情况下,如果事务不存在,则不会调用 restoreTransaction 方法中的第二个 UIAlertView

我已经设置了一个断点,但它永远不会被调用。我已经设置了SKPaymentTransactionStateFailed 案例的断点,我看到了,当我没有积极购买时,它会受到打击。但是,这实际上会从AppDelegate 中执行我想要的(如UIAlertView),我只希望它在单独的InAppPurchaseViewController 中运行。

问题

从这个InAppPurchaseViewController(距离几个屏幕),我有一个恢复UIButton,我想要在单击按钮期间执行测试以查看此Apple ID 的交易存在。这样的事情可能吗?

更新

SKPaymentTransactionStateFailed,我放了:

        NSString * const SKErrorDomain;
        NSLog(@"The error is %@", SKErrorDomain);

我收到(null) 作为错误。

【问题讨论】:

    标签: ios objective-c iphone uibutton in-app-purchase


    【解决方案1】:

    尝试添加此方法,它处理没有购买可恢复的情况:

    - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    
       if (queue.transactions.count == 0){
         NSLog(@"The transaction does not exist");
    
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Restore Unsuccessful" message:@"Your Apple ID doesn't have an active purchase for upgrading to Envylope Pro. " delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil, nil];
         [alertView show];
     }
    }
    

    【讨论】:

    • 非常感谢您的回复 - 知道有该方法可用真是太好了。我是否需要从案例 SKPaymentTransactionStateFailed 或类似的地方调用它?对不起,我问的原因是因为它目前没有用那个方法做任何事情
    • 它是 SKPaymentTransactionObserver 协议的一部分,因此当您从 App Store 收到响应时会自动调用它。 Xcode 说:“在支付队列处理完所有可恢复的交易后调用此方法。”尝试在真实设备上进行测试,因为模拟器通常会出错。您可以实现 paymentQueue:restoreCompletedTransactionsFailedWithError: 来检查它是否为真。
    • 非常感谢 - 我运行了一些调试程序,但由于某些奇怪的原因,它没有在 App Delegate 中调用。很抱歉问,我将如何实现 restoreCompletedTransactionsFailedWithError?对不起,有点像新手!
    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多