Contents

purchase(options:)

Initiates a purchase for the product with the App Store and displays the confirmation sheet.

Declaration

@MainActor func purchase(options: Set<Product.PurchaseOption> = []) async throws -> Product.PurchaseResult

Parameters

  • options:

    A set of options you can associate with the purchase.

Mentioned in

Return Value

Returns a Product.PurchaseResult.

Discussion

StoreKit provides several APIs you can use to enable customers to initiate a purchase. Before using purchase(options:) consider the following APIs and choose the one that best suits your app’s implementation:

Use the purchase API

Call the purchase(options:) method when a customer initiates a purchase, either within your app or after selecting a promoted in-app purchase on the App Store. This method brings up the system-confirmation sheet. The user can confirm to complete the transaction or cancel it.

Include the purchase options to provide additional information about the purchase, such as:

The following example illustrates calling purchase(options:) using the options parameter to provide an app account token:

let appAccountToken = <# Generate an app account token. #>
let purchaseResult = try await product.purchase(options: [
    .appAccountToken(appAccountToken)
])

If you’re testing your app in the sandbox environment, test an Ask to Buy scenario by setting the simulatesAskToBuyInSandbox(_:) purchase option to true. For more information about Ask to Buy, see Approve what kids buy with Ask to Buy.

This method may throw a Product.PurchaseError or StoreKitError.

For more information about purchases that users initiate on the App Store, see Promoting In-App Purchases.

See Also

Purchase requests and results