Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to check subscribed on first View #22

Closed
sinkcup opened this issue Nov 15, 2022 · 1 comment
Closed

how to check subscribed on first View #22

sinkcup opened this issue Nov 15, 2022 · 1 comment
Labels
works as intended This feature is working as designed

Comments

@sinkcup
Copy link

sinkcup commented Nov 15, 2022

I want show content or notice subscription on the App's first View, so I need check subscribed first.

but storeHelper.start() is a async function, so the storeHelper.subscriptionProductIds is empty, and can not check subscribed.

image

@russell-archer
Copy link
Owner

russell-archer commented Nov 16, 2022

Yes, you can't things the way you're attempting it. I would suggest having storeHelper.start() in your @main App struct and then have checks for products being purchased or subscribed in the first view that's displayed.

For example, see SimplePurchaseView() in StoreHelperDemo (https://github.com/russell-archer/StoreHelperDemo). That view could be called directly from your App struct. SimplePurchaseView() shows how to check for a non-consumable product purchase but the same thing would work for a subscription. Something like this:

@main struct YourApp: App {
    @StateObject var storeHelper = StoreHelper()
    
    var body: some Scene {
        WindowGroup {
            YourFirstView().environmentObject(storeHelper)
                .task { storeHelper.start() }
        }
    }
}

struct YourFirstView: View {
    @EnvironmentObject var storeHelper: StoreHelper
    @State var purchaseState: PurchaseState = .unknown
    let productId = "com.rarcher.nonconsumable.flowers.large"
    
    var body: some View {
        VStack {
            // Product details here...
            
            if purchaseState == .purchased {
                Text("This product has already been purchased").multilineTextAlignment(.center)
            } else {
                Text("This product is available for purchase").multilineTextAlignment(.center)
            }
            
            Spacer()
        }
        .padding()
        .task {
            let purchased = (try? await storeHelper.isPurchased(productId: productId)) ?? false
            purchaseState = purchased ? .purchased : .unknown
        }
    }
}

@russell-archer russell-archer added the works as intended This feature is working as designed label Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
works as intended This feature is working as designed
Projects
None yet
Development

No branches or pull requests

2 participants