Testing In-App Purchases

Hi,

I have a couple of questions in regards to testing in-app purchases. I tested the subscription on a device but I'm not sure how to reset so I can test again. I didn't see the subscription in device settings or in Debug -> StoreKit -> Manage Subscriptions window. Additionally, I was wondering if there was a way to detect the subscription being made. I implemented this, but I'm not sure if that will work:

.onChange(of: Product.SubscriptionInfo.RenewalState.subscribed) {
    if Product.SubscriptionInfo.RenewalState.subscribed == .subscribed {
    }
}

Hi, when testing in-app purchases on device, it will depend on which testing environment you are in to clear purchase history. If using StoreKit testing in Xcode, as you mentioned, go to Xcode > Debug menu > StoreKit > Manage Transactions to see, edit, and delete purchases. If using Sandbox testing with a Sandbox account go to Settings on your device > Developer > Sandbox Apple Account > Manage > Clear Purchase History. You can also clear purchase history for Sandbox accounts on App Store Connect. Learn more about StoreKit testing here: Testing at all stages of development with Xcode and the sandbox.

To your second question, Product.PurchaseResult will give you the result after calling purchase(). You should also use Transaction.updates to listen for new transactions as soon as your app launches, in case of out of app purchases.

More info: https://developer.apple.com/documentation/storekit/implementing-a-store-in-your-app-using-the-storekit-api

With my implementation, I am just using SubscriptionStoreView() so I wasn't able to apply the link you shared. I tried something like this:

.onChange(of: Product.PurchaseResult.success(<#VerificationResult<Transaction>#>)) {

}

However, I get the error: Instance method 'onChange(of:initial:_:)' requires that 'Product.PurchaseResult' conform to 'Equatable'

Also, I'm not sure what the parameter VerificationResult is and how to access it.

Just following up regarding to the question in my previous post. Thank you.

@marcofusco111 As mentioned above, you should also useTransaction.updates to listen for new transactions as soon as your app launches, in case of out of app purchases. You can iterate through all the result to check the purchases. Additionally, use SubscriptionStatus.updates to check the status of the subscription:


for await status in SubscriptionStatus.updates {
       // Verify the transaction 
       let transaction = try await self.checkVerified(status.transaction)
       // Get the product identifier of the current In-App Purchase
       let productID = transaction.productID
       // The status of the In-App Purchase
       let state = status.state
         // Handle the state and update your app's UI as appropriate.
}

Hi,

I'm trying to implement the code provided in a function within an async function in ContentView. However, I get an error "Value of type 'ContentView' has no member 'checkVerified'"

let transaction = try await self.checkVerified(status.transaction)

In this example, checkVerified is a function implemented in the Store.swift class of the Implementing a store in your app using the StoreKit API sample code. The function verifies a transaction. For more information, see Product.PurchaseResult.

When trying to use Product.PurchaseResult or Transaction.updates, or SubscriptionStatus.updates within an .onChange function I get the error:

"Instance method 'onChange(of:initial:_:)' requires that 'Transaction.Transactions' conform to 'Equatable'"

Also, despite clearing purchase history in settings it doesn't seem that the subscription is resetting.

Testing In-App Purchases
 
 
Q