I have a Shortcuts action via an App Intent that I want only for active subscribers to use.
I have a shared class that handles all the subcription related things. But for some reason my code only works if the app is active in the background. Once the app is quitted and the user performs the Shortcut, the not subscribed error is thrown – even though the user is subscribed.
How can I ensure that my subscription check is done correctly, if the app isn’t open in the background?
My Code
App Intent excerpt:
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<MeterIntentEntity> {
// Validate that the user is subscribed.
// Cancels action with error message if not subscribed.
if SubscriptionManager.shared.userIsSubscribed == false {
throw IntentError.notSubscribed
}
// More Code …
// Finish and pass created value as result.
return .result(value: something)
}
Subscription Manager excerpt:
class SubscriptionManager: ObservableObject {
// A singleton for our entire app to use
static let shared = SubscriptionManager()
let productIds = ["my_sub1", "my_sub2"]
@Published private(set) var availableSubscriptions: [Product]
@Published private(set) var purchasedSubscriptions: [Product] = []
public var userIsSubscribed: Bool {
return !self.purchasedSubscriptions.isEmpty
}
init() {
// Initialize empty products, and then do a product request asynchronously to fill them in.
availableSubscriptions = []
Task {
await updatePurchasedProducts()
}
}
@MainActor
func updatePurchasedProducts() async {
for await result in Transaction.currentEntitlements {
do {
let transaction = try checkVerified(result)
if let subscription = availableSubscriptions.first(where: { $0.id == transaction.productID }) {
purchasedSubscriptions.append(subscription)
}
} catch {
Logger.subscription.error("Error loading users user's purchased products.")
}
}
}