I'm trying to display a Subscribe Now modal view instantly after the app starts to encourage users to subscribe to the Pro In-App Purchase, so I used the .onAppear
modifier, and it works fine only if I want to show the modal every time the app starts.
struct ContentView: View {
@State private var selection: String? = nil
@State private var showModal = false
@ObservedObject var storeManager: StoreManager
var body: some View {
NavigationView {
VStack {
// Contents Here
}
}
.onAppear {
self.selection = "Pro"
self.showModal.toggle()
}
.sheet(isPresented: $showModal) {
if self.selection == "Pro" {
Pro(showModal: self.$showModal, storeManager: self.storeManager)
.onAppear(perform: {
SKPaymentQueue.default().add(storeManager)
})
}
}
}
}
Now, the problem begins when I want to display the modal only to those who have not subscribed yet to the Pro IAP, so I modified .onAppear
to:
.onAppear {
ForEach(storeManager.myProducts, id: \.self) { product in
VStack {
if !UserDefaults.standard.bool(forKey: product.productIdentifier) {
self.selection = "Pro"
self.showModal.toggle()
}
}
}
}
But, the if
and ForEach
seems not to work smoothly with structs and views. How should I use them in my case?
CodePudding user response:
You're trying to use View
code in onAppear
, which doesn't accept a View
-- it should just be regular imperative Swift code. So, you likely want something like this:
.onAppear {
storeManager.myProducts.forEach { product in
if !UserDefaults.standard.bool(forKey: product.productIdentifier) {
self.selection = "Pro"
self.showModal.toggle() //maybe change this to = true -- toggle could *close* it again if there were multiple items that qualified
}
}
}
CodePudding user response:
be careful, on your .onAppear{}, it should be Swift code instead SwiftUI (ForEach, VStack are view structs)