An action that starts an in-app purchase.
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)
}
}
}
Starts an in-app purchase for the given product, with the given purchase options.
Don't call this method directly. SwiftUI calls it when you
call the ``PurchaseAction`` structure that you get from the
``Environment``, using the product and purchase options as arguments:
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)
}
}
}
- Parameters:
- product: The product to purchase.
- options: A set of options to configure the purchase.
- Returns: The result of the purchase
- Throws: A `PurchaseError` or `StoreKitError`.