Storekit Restore委托方法多次调用

时间:2018-06-12 06:15:36

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

我已在我的应用中实施应用内购买。我的问题是当新用户点击恢复按钮时,完成1个循环后进程没有停止并反复执行。执行3到4次后,进程停止。

我想在新用户没有购买任何交易时立即停止处理。

这是我的IAP课程的代码。

- (void)restorePurchases
{

  [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    [ProgressHUD dismiss];
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    [ProgressHUD dismiss];
}

// Sent when transactions are removed from the queue (via finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions
{

    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    [ProgressHUD dismiss];
}

// Called when the transaction status is updated
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

        for (SKPaymentTransaction *transaction in transactions) {
            switch (transaction.transactionState)
            {
                case SKPaymentTransactionStatePurchasing:
                    [self transactionStart:transaction];
                    break;

                case SKPaymentTransactionStatePurchased:
                    [self transactionComplete:transaction];
                    [[SKPaymentQueue defaultQueue]
                     finishTransaction:transaction];
                    break;

                case SKPaymentTransactionStateFailed:
                    [self transactionFailed:transaction];
                    [[SKPaymentQueue defaultQueue]
                     finishTransaction:transaction];
                    break;
                case SKPaymentTransactionStateRestored:
                {
                    [self transactionRestored:transaction];
                    [[SKPaymentQueue defaultQueue]
                     finishTransaction:transaction];
                    break;
                }
                default:
                    break;
            }
        }

}

#pragma mark - Local method for mange transactions -

-(void)transactionStart:(SKPaymentTransaction *)transaction
{
    if([self.delegate respondsToSelector:@selector(startPayment:)]){
        [self.delegate startPayment:transaction];
    }
}

-(void)transactionComplete:(SKPaymentTransaction *)transaction
{


    if([self.delegate respondsToSelector:@selector(completePayment:)]){
        [self.delegate completePayment:transaction];
    }

}

-(void)transactionFailed:(SKPaymentTransaction *)transaction
{

    if ([self.delegate respondsToSelector:@selector(failedPayment:)]) {
        [self.delegate failedPayment:transaction];
    }

}

-(void)transactionRestored:(SKPaymentTransaction *)transaction
{

    if([self.delegate respondsToSelector:@selector(restoredPayment:)]){
         [[AppDelegate sharedAppDelegate].arrayConacatResponse addObject:[NSString stringWithFormat:@"transactionRestored method "] ];
        [self.delegate restoredPayment:transaction];
    }
}

In Restore payment method 


- (void)restoredPayment:(SKPaymentTransaction *)transactions
{

    if ([transactions.payment.productIdentifier isEqualToString:monthlySubscription]) {

        NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
        NSError *receiptError;
        BOOL isPresent = [receiptURL checkResourceIsReachableAndReturnError:&receiptError];

        if (isPresent)
        {
            NSData *receiptData = [[NSData alloc] initWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];

            BOOL isSubscriptionPeriodValid = [[DataBase sharedInstance] getSubscriptionStatusFromAppleWithReceipt:receiptData];
            if (isSubscriptionPeriodValid)
            {


                [self removeFromSuperview];
            }else{
                // Here I want to stop

                [CommonUtilities showAlertWithTitle:@"" Message:@"You have no transaction to restore."];

            }
        }


    }

    [ProgressHUD dismiss];
}

0 个答案:

没有答案