I'm working on a SwiftUI app where I need to integrate subscription status checks using subscriptionStatusTask in a TabView. I've attempted to implement this in my AppTabView, but I'm encountering issues where the subscription status task does not seem to function as expected within the TabView. It works perfectly when applied to other views.
Here's a brief outline of my approach:
I have a TabView setup in AppTabView, which is the main view of my app. Each tab is supposed to check the subscription status upon selection. However, the subscriptionStatusTask does not seem to trigger or update the view when I switch between tabs. I've tried adding .subscriptionStatusTask within the TabView block, but this hasn't resolved the issue. Here's a simplified version of my code structure:
struct AppTabView: View {
// Environment objects and state variables
var body: some View {
TabView(selection: $selectedTab.selected) {
DashboardView(resetState: $resetDashboardState)
.tabItem { Label("Home", systemImage: "house.fill") }
.tag(0)
// Other tabs...
}
.subscriptionStatusTask(for: passIDs.group) { taskStatus in
// Handle subscription status
}
// Other modifiers...
}
}
Has anyone encountered a similar issue or knows how to correctly implement subscriptionStatusTask within a TabView? Any guidance on how to ensure the subscription status is checked and the view is updated when switching tabs would be greatly appreciated.
Does this solve your problem?
import SwiftUI import StoreKit
struct ContentView: View {
@State private var paywallShown = true
@State private var isPro = false
var body: some View {
Text("Demo")
.sheet(isPresented: $paywallShown) {
PaywallView()
}
.subscriptionStatusTask(for: "506F71A6") { taskState in
if let value = taskState.value {
isPro = !value
.filter { $0.state != .revoked && $0.state != .expired }
.isEmpty
} else {
isPro = false
}
}
}
}
struct PaywallView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
SubscriptionStoreView(groupID: "506F71A6")
.onInAppPurchaseStart { product in
print(product.displayName)
}
.onInAppPurchaseCompletion { product, result in
dismiss()
}
}
}
#Preview {
ContentView()
}