How to know if a product has been purchased

Hi guys.


Apple is asking me that if a Non-Consumable product has been purchased by a user and the user decides to delete and reinstall the App, then I must present a "Restore" instead than "Buy" button.

To check if a product has been purchased I am using NSUserDefaults, like this:


   public init(productIds: Set<ProductIdentifier>) {
     
      self.productIdentifiers = productIds
     
      for productIdentifier in productIds {
         let purchased = NSUserDefaults.standardUserDefaults().boolForKey(productIdentifier)
         if purchased {
            purchasedProductIdentifiers.insert(productIdentifier)
            print("Previously purchased: \(productIdentifier)")
         } else {
            print("Not purchased: \(productIdentifier)")
         }
      }
     
      super.init()
     
      SKPaymentQueue.defaultQueue().addTransactionObserver(self)
   }


But, if the user deletes the App, this information is lost.


So, Is there anyway to ask to SKPayment if a product has been purchase? I guess there is because they are asking me that. But I didnt find it.

Replies

There is a method called restoreCompletedTransactions that you will implement which will cause StoreKit to send a transaction to your updatedTransactions method. You could also restore the app's receipt and then decode that receipt. Both of these actions will require that the user log into their iTunes Account thereby 'proving' who they are.


But there is another issue for you to consider. It is quite easy for one of your users to access their NSUserDefaults file and discover a variable called 'productIdentifier' and set it to "YES". You may want to change that to a variable like a hash of the user's identifierForVendor and see if they have stored the correct value for that variable in their NSUserDefaults file. That way they will be unable to falsely claim they have purchase your IAP.

How we handle following situation?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/#//apple_ref/occ/instp/UIDevice/identifierForVendor



The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them.

Mobiledatabooks,


You don't understand the purpose of using identifierForVendor. Reread my post. When the user deletes the app they will also delete the NSUserDefault that contains the (now incorrect) identifierForVendor.

Not likely to happen as most iPhone users are as dumb as a box of hammers.