StoreKit: Catch failed restore?

后端 未结 3 1538
星月不相逢
星月不相逢 2021-01-04 18:59

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

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

相关标签:
3条回答
  • 2021-01-04 19:12

    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

    0 讨论(0)
  • 2021-01-04 19:21

    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....

    0 讨论(0)
  • 2021-01-04 19:31

    You can use following delegate method for Restore Transaction.

     -(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error{
    NSLog(@"Error when purchasing: %@",error);
    
    }
    
    0 讨论(0)
提交回复
热议问题