we are deveope an app on VisionOS IAP, we tried in app purchase code example on iphone, but seems not work, Xcode tell me: purchase(options:) is unavailable in visionOS: use @Environment(.purchase) to get a PurchaseAction value to call. If your app uses UIKit, use purchase(confirmin:options:)." 1.Anybody know how to solve this and give us any help? and we already searched on tutorials and forum,seems no result. Thankyou very much!
Vision Pro In app purchase
Hey @dongke,
As the error suggests, you need to use @Environment(.purchase)
to get a PurchaseAction
to start an In-App Purchase. The documentation provides a great example of this in use.
This was introduced in iOS 17, so if you are supporting devices earlier than this you will need to use both approaches in your codebase.
You can refactor your function to the following to take the purchase result that you receive from the environment value:
func processPurchaseResult(_ result: Product.PurchaseResult) async throws -> Transaction? {
switch result {
…
If previously inside of your view you were just checking against the result of your purchase(:)
function, you will want to call the PurchaseAction
and then pass that to the updated processPurchaseResult
:
if
let purchaseResult = try? await purchase(product),
try await store.processPurchaseResult(purchaseResult) != nil
{
Let me know if you have additional questions,
Michael