如何为iOS中的非消耗性产品还原IAP

时间:2018-11-28 11:50:08

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

我想知道 是否必须将两个按钮分开放置,一个用于升级,另一个用于还原,以恢复以前的交易?

我本人应采取哪些步骤以避免拒绝App for Restore交易的机会。

我将以下代码用于还原非消耗性的应用内购买。 如果以下代码有任何更改,请告诉我。

- (void)buyProduct:(SKProduct *)product {

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
        //[[SKPaymentQueue defaultQueue] addPayment:payment];
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

}

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

    for (SKPaymentTransaction * transaction in transactions) {

        switch (transaction.transactionState)
        {
                // Call the appropriate custom method for the transaction state.
            case SKPaymentTransactionStatePurchasing:
                [self showTransactionAsInProgress:transaction deferred:NO];
                break;
            case SKPaymentTransactionStateDeferred:
                [self showTransactionAsInProgress:transaction deferred:YES];
                break;
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                // For debugging
                NSLog(@"Unexpected transaction state %@", @(transaction.transactionState));
                break;
        }
    };


}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    NSLog(@"Restore Completed Transactions Failed WithError...%@",error);
    [self failedMessage:@"Restore Completed Transactions Failed"];
    [self stopIndicator];
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSMutableArray *arrPurchasedItemIDs = [[NSMutableArray alloc] init];
    for (SKPaymentTransaction *transaction in queue.transactions)
    {
        NSString *productID = transaction.payment.productIdentifier;

        [arrPurchasedItemIDs addObject:productID];

        NSLog(@"arrPurchasedItemIDs : %@",arrPurchasedItemIDs);
    }

    NSLog(@"Restore Completed");
    [self completeMessage:@"Restore Completed"];
    [self stopIndicator];
}

1 个答案:

答案 0 :(得分:1)

在这里您可以分开恢复购买代码:

-(void)doClickRestore {
    [APP_DEL doStartSpinner];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
                // Purchase success Transaction
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
                // Purchase fail Transaction
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
            {
                [self doStopSpinner];
                [self restoreTransaction:transaction];
            }
            default:
                break;
        }
    }
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction {
    [self doStopSpinner];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

注意:

点击“还原购买”将还原所有购买的非消耗性inApp购买

希望这会有所帮助。

下面两个方法是可选的,但是如果您愿意,可以使用它。

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


- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue