StoreKit: Catch failed restore?

2019-02-12 22:29发布

I'm implementing In-App purchase feature with Restore button.

I have a brand new test user set up, without any payments made.

When I hit the restore button, and log in with the new test user, I cannot catch any delegated methods that tell me that the restoring transaction has failed (since there is nothing to restore).

The only method that get invoked is -(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue, but this method gets called in the case when restoring was successful too.

What can I do now? How can I catch such a case?


Addition: I have a progress indicator that says "Contacting App Store", and I need an invocation where I can hide it in failed cases too.

3条回答
Ridiculous、
2楼-- · 2019-02-12 22:51

You can use following delegate method for Restore Transaction.

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

}
查看更多
地球回转人心会变
3楼-- · 2019-02-12 22:59

Noah: here's a code snippet for you:

-(void)restore {

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


}

And the following method:

-(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];

}

Let me know if that helps you....

查看更多
时光不老,我们不散
4楼-- · 2019-02-12 23:09

When you restore a transactions, there're two delegated methods:

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

The first one (paymentQueueRestoreCompletedTransactionsFinished) is called when all the transactions are restored. If you don't have any previous purchase it also calls this method because the restored worked fine but there's nothing to restore.

The other method (restoreCompletedTransactionsFailedWithError) is called when there's an error restoring the transaction.

If you need to show a message to the user telling him he doesn't have any transaction to restore you can use:

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

Here you have a small snippet for this delegate:

//
// 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;
        }
    }
}

Then you can use the restoredTransaction variable to know if any transaction has been restored on paymentQueueRestoreCompletedTransactionsFinished

查看更多
登录 后发表回答