StoreKit:Catch恢复失败?

时间:2011-10-13 13:33:23

标签: iphone in-app-purchase storekit

我正在使用还原按钮实现应用内购买功能。

我设置了全新的测试用户,没有任何付款。

当我点击恢复按钮并使用新测试用户登录时,我无法捕获任何委托方法,告诉我恢复事务已失败(因为没有任何要恢复的内容)。

调用的唯一方法是-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue,但在恢复成功的情况下会调用此方法。

我现在能做什么?我怎么能抓到这样的案子?


另外:我有一个进度指示器,上面写着“联系App Store”,我需要一个调用,我可以在失败的情况下隐藏它。

3 个答案:

答案 0 :(得分:8)

诺亚:这是给你的代码片段:

-(void)restore {

    [self.delegate showLoadingScreen];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];


}

以下方法:

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    if (!queue.transactions || [queue.transactions count] == 0) {

        [self showAlertScreenWithTitle:@"Error Restoring Subscription" message:@"No subscription was found to restore."];

    } else {

        BOOL didRestore = NO;

        for (SKPaymentTransaction *t in queue.transactions) {

            if (t.transactionState == SKPaymentTransactionStateRestored || t.transactionState == SKPaymentTransactionStatePurchased) {

                NSTimeInterval now = [[NSDate date] timeIntervalSince1970] - _timeOffset;
                NSTimeInterval expire = [t.transactionDate timeIntervalSince1970] + kExplorerSubscriptionSecondLength;
                NSTimeInterval purchase = [t.transactionDate timeIntervalSince1970];

                if (purchase <= now && now <= expire) {

                    didRestore = YES;
                }

            }


        }

        if (!didRestore)
            [self showAlertScreenWithTitle:@"Error Restoring Subscription" message:@"No subscription was found to restore."];

    }    

    [self.delegate dismissLoadingScreen];

}

如果有帮助,请告诉我....

答案 1 :(得分:1)

恢复事务时,有两种委派方法:

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error

恢复所有交易后,将调用第一个( paymentQueueRestoreCompletedTransactionsFinished )。如果您之前没有任何购买,它也会调用此方法,因为恢复的工作正常,但没有什么可以恢复。

恢复事务时出现错误,将调用另一种方法( restoreCompletedTransactionsFailedWithError )。

如果您需要向用户显示一条消息,告诉他他没有任何要恢复的交易,您可以使用:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

这里有一个代表小片段:

//
// called when the transaction status is updated
//
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                restoredTransaction++;
                break;
            default:
                break;
        }
    }
}

然后,您可以使用 restoredTransaction 变量来了解是否已在 paymentQueueRestoreCompletedTransactionsFinished 上恢复任何交易

答案 2 :(得分:0)

您可以使用以下委托方法进行恢复交易。

 -(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error{
NSLog(@"Error when purchasing: %@",error);

}