Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paymentQueue:updatedTransactions gets called when it shouldn't?

I'm having a weird problem with an in-app purchase...

After I'm requesting the product information, sometimes the paymentQueue:updatedTransactions gets called automatically for some reason.

In my store's viewDidLoad method, I initialize and start the request:

- (void)viewDidLoad
{
    [super viewDidLoad];

    productsArray = [[NSArray alloc] init];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSSet *productIdentifiers = [NSSet setWithObjects:PRODUCT_ID, nil];
    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];
}

Then I get a response:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    productsArray = [[NSArray alloc] initWithArray:response.products];

    NSLog(@"Products Count: %d",[productsArray count]);
    NSLog(@"Invalid Products Count: %d",[response.invalidProductIdentifiers count]);

    if ([productsArray count] > 0)
    {
        NSLog(@"Product title: %@" ,[productsArray objectAtIndex:0]);
        [self hideLoadingView];


    } else {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No products found"
                                                            message:@"There might have been a problem. Please try again soon." delegate:self 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];

        [self hideLoadingView];
        [purchaseBtn setEnabled:NO];

    }

    [productsRequest release];

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];

}

This is where I expect the process to end, and wait for the user to tap the buy button... but sometimes (like, 70% of the times), I get a username/password alert box pop-up in order to buy the item... but the user didn't tap anything... (and if the user is already "logged in", then it buys the item without asking. Which is NOT how it should be.

This is the method that is being called automatically sometimes:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"updated transaction");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                NSLog(@"transationStatePurchased");
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                NSLog(@"transationStateFailed");
                [self failedTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

and this is the IBAction of the buy button:

-(IBAction)buyItem:(id)sender {

    [self showLoadingView];

    SKPayment *payment = [SKPayment paymentWithProductIdentifier:[(SKProduct *)[productsArray objectAtIndex:0] productIdentifier]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];

    }        
}

I thought that the user/password alert box of StoreKit shouldn't get displayed until I call the defaultQueue's "addPayment" method, which appears ONLY in the IBAction.

Any ideas? Thanks!

like image 458
itai alter Avatar asked Feb 22 '23 05:02

itai alter


1 Answers

Once you add a transaction observer iOS will check the default Queue if there is any non completed transaction (which mean you didn't finish it) so it will show the alert box every time there is a transaction until you finish it even if you didn't do any action to add a new payment.

like image 150
Malek_Jundi Avatar answered Mar 24 '23 21:03

Malek_Jundi