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.PurchaseResultParameters
- 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 PurchaseAction for apps that use SwiftUI on any platform, including multi-scene apps for visionOS.
Use purchase(confirmIn:options:) for apps that use UIKit.
Use purchase(confirmIn:options:) for apps that run on macOS and use AppKit.
Use purchase(options:) for apps that runs on watchOS.
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:
appAccountToken(_:) to associate the purchase with the resulting transaction
promotionalOffer(offerID:keyID:nonce:signature:timestamp:), if the customer is redeeming a promotional offer for an auto-renewable subscription
quantity(_:), if the customer is purchasing more than one of the product
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.