Storekit Восстановление метода делегата, вызванного несколько раз - PullRequest
0 голосов
/ 12 июня 2018

Я реализовал внутриигровую покупку в своем приложении.Моя проблема заключается в том, что когда новый пользователь нажимает кнопку восстановления, процесс не останавливается после завершения 1 цикла и выполняется снова и снова. После выполнения 3-4 раза процесс останавливается.

Iхочу немедленно остановить процесс, когда у нового пользователя нет купленной транзакции.

Вот код моего класса 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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...