user155
user155

Reputation: 805

Check if SKProduct is already purchased

Android Google Billing In-app gives all information about product (including if it's purchased or not)

What about iOS In-App Purchase (IAP)?

I don't want to save purchased items locally (UserDefaults for iOS) or to my own server after user bought it

Android uses Google Play Services, AIDL to get already purchased items even in offline mode

For safety in Android we can validate if user really bought item (and didn't use some hack app to get it for free) - https://developer.android.com/google/play/billing/billing_best_practices#validating-purchase-server

This additional validation for already purchased items we can do when user's device internet is enabled (though it should be enough to check it only during buying item), if internet is disabled we can temporary say that user bought it (but if he didn't buy it yet then internet is required anyway, we will validate it on our server anyway in this case)

So how does iOS work then? Does it have some, I don't know, iTunes Services to get IAP information for a specific app?

Upvotes: 5

Views: 3116

Answers (2)

The answer is to check the receipt for the application - you are supposed to send the receipt up to Apple through your own servers to validate, but you can also parse the StoreKit receipt locally. There you can find prior purchases made.

Upvotes: 1

Igor N
Igor N

Reputation: 381

Yes, on iOS you should use the StoreKit to fetch the details about all of the in-app purchases available for that app.

Once you setup your in-app purchases in App Store connect you will have the purchase ids.

To retrieve the information about them you should make an SKProductRequest using the purchase ids. This will return the required information.

import StoreKit

class YourPurchaseManagerClass: NSObject {
  var productsRequest: SKProductsRequest?
  var products: [SKProduct]?

  func validateProductIdentifiers(productIdentifiers: Set&ltString>) {
    // Create and start the 
    productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
    productsRequest?.delegate = self
    productsRequest?.start()
  }
}

extension YourPurchaseManagerClass: SKProductsRequestDelegate {

  func productsRequest(_ request: SKProductsRequest,
                       didReceive response: SKProductsResponse) {

    // Store the products data. This will contain the information about the purchases
    products = response.products;

    for invalidIdentifier in response.invalidProductIdentifiers {
      // Handle any invalid product identifiers.
    }
  }
}

Here is a link that talks specifically about retrieving the in-app purchases information and includes sample code.

Here is a link to Apple documentation about the whole in-app purchase setup process

Upvotes: -3

Related Questions