updates
The asynchronous sequence that emits a transaction when the system creates or updates transactions that occur outside the app or on other devices.
Declaration
static var updates: Transaction.Transactions { get }Mentioned in
- Supporting win-back offers in your app
- Supporting offer codes in your app
- Supporting subscription offer codes in your app
- Testing win-back offers in the sandbox environment
- Testing purchases made outside your app
- Getting started with In-App Purchase using StoreKit views
- Merchandising win-back offers in your app
- Supporting monthly subscriptions with a 12-month commitment
Discussion
Use updates to receive new transactions while the app is running. This sequence receives transactions that occur outside of the app, such as Ask to Buy transactions, offer code redemptions, and purchases that customers make in the App Store. It also emits transactions that customers complete in your app on another device.
Note that after a successful in-app purchase on the same device, StoreKit returns the transaction through Product.PurchaseResult.success(_:).
The following example shows a class that creates a Task when it initializes. The task retrieves and processes any unfinished transactions.
final class TransactionObserver {
var updates: Task<Void, Never>? = nil
init() {
updates = newTransactionListenerTask()
}
deinit {
// Cancel the update handling task when you deinitialize the class.
updates?.cancel()
}
private func newTransactionListenerTask() -> Task<Void, Never> {
Task(priority: .background) {
for await verificationResult in Transaction.updates {
self.handle(updatedTransaction: verificationResult)
}
}
}
private func handle(updatedTransaction verificationResult: VerificationResult<Transaction>) {
guard case .verified(let transaction) = verificationResult else {
// Ignore unverified transactions.
return
}
if let revocationDate = transaction.revocationDate {
// Remove access to the product identified by transaction.productID.
// Transaction.revocationReason provides details about
// the revoked transaction.
<#...#>
} else if let expirationDate = transaction.expirationDate,
expirationDate < Date() {
// Do nothing, this subscription is expired.
return
} else if transaction.isUpgraded {
// Do nothing, there is an active transaction
// for a higher level of service.
return
} else {
// Provide access to the product identified by
// transaction.productID.
<#...#>
}
}
}The updates listener receives unfinished transactions just once at app launch, but you can use the unfinished listener to get your app’s unfinished transactions at any time. For information on finishing transactions, see finish().