purchase
An action that starts an in-app purchase.
Declaration
@MainActor @preconcurrency var purchase: PurchaseAction { get }Discussion
Read this environment value to get an PurchaseAction instance for a given Environment. Call the instance to start an in-app purchase. You call the instance directly because it defines a PurchaseAction/callAsFunction(_:options:) method that Swift calls when you call the instance.
For example, you can start an in-app purchase when the user taps a button:
struct PurchaseExample: View {
@Environment(\.purchase) private var purchase
let product: Product
let purchaseOptions: [Product.PurchaseOption]
var body: some View {
Button {
Task {
let purchaseResult = try? await purchase(product, options: purchaseOptions)
// Process purchase result.
}
} label: {
Text(product.displayName)
}
}
}