iOS StoreKit - When to call - (void)restoreCompletedTransactions?

后端 未结 5 540
一个人的身影
一个人的身影 2021-01-11 23:41

I have lots of 1 time purchase IAPs inside of my application. Users can purchase them just fine.

My problem is that I am integrating with Flurry to track real purch

5条回答
  •  礼貌的吻别
    2021-01-12 00:01

    EDIT:

    Originally I had posted a very long, unneeded method to get what I needed done, but as you will see below, Matt helped me figure out the variable I was looking for.

    For an example, let's imagine a user previously purchased my app, bought all of the non-consumable IAP's available, then deleted the app. When the user reinstalls the app, I want to be able to determine when they go to "purchase" the products again, will it be an original (first time) purchase, or a restoration purchase?

    I have implemented a "Restore all purchases" button, but let's say the user ignores/does not see it, and tries to select a product they have purchased before.

    As with a normal purchase, I do the following:

    if ([SKPaymentQueue canMakePayments])
    {
          self.productRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productID]];
    
          self.productRequest.delegate = self;
          [self.productRequest start];
    }
    else
    {
         //error message here
    }
    

    After the user has logged into their iTunes account, the App will let them know they have already purchased this and it will now be restored. The following delegate method will be called:

     -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
    {   
        for (SKPaymentTransaction *transaction in transactions)
        {
            switch (transaction.transactionState)
            {
                case SKPaymentTransactionStatePurchased:
                {
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                    if(transaction.originalTransaction)
                    {
                        NSLog(@"Just restoring the transaction");
                    }
                    else
                    {
                        NSLog(@"First time transaction");
                    }
    
                    break;
                }
                default:
                    break;
            }
        }
    }
    

    No matter if the transaction was a restoration or a first time purchase, transaction.transactionState is going to be equal to SKPaymentTransactionStatePurchased.

    Now from this point how do we determine if that purchase is an original or restoration purchase?

    Simple: As seen above, just see if transaction.originalTransaction is initialized. Per Apple's note: // Only valid if state is SKPaymentTransactionStateRestored.

    If the SKPaymentTransaction's originalTransaction is initialized, that means that there was a previous transaction. Otherwise, this transaction is an original one!

    Once again, thanks to Matt for pointing me in the right direction, and for making the logic a lot cleaner!

提交回复
热议问题