UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Change initial screen based on a variable

Forums > SwiftUI

I have my app which requires a license to use properly, so when the app loads up I check to see if they have the license, if so they go to the main ContentView, if not you go to LockedView. I am using the Purchases framework to manage in the In App Purchases.

@main
struct RugbyAppApp: App {
    @ObservedObject var userSettings = UserSettings.shared

    var swiftUISpeech = SwiftUISpeech()

    init(){
        Purchases.debugLogsEnabled = true
        Purchases.configure(withAPIKey: "UIGTHCnzIZUPvnxevAxUGTCchtpIpYZh")
        userSettings.checkSubscription()
    }

    var body: some Scene {
        WindowGroup {
            if(userSettings.license == true){
                ContentView().id(appState.gameID)
            }
            else{
                LockedView()
            }
        }
    }
}

Inside the initialiser I run userSettings.checkSubscription() which is this:

class UserSettings: ObservableObject {

    static let shared = UserSettings()

    @Published var license: Bool

    init() {
        //Other variables get initialised in here too
        self.license = false
    }

    func checkSubscription(){
        Purchases.shared.purchaserInfo { (info, error) in
            if info?.entitlements["subscribed"]?.isActive == true{
                self.license = true
            }
            else{
                self.license = false
            }
        }
    }
}

If the transaction is completed, and the Purchases frameworks receives the message saying everything has gone through I just update the license like this:

userSettings.license = true

I want the app to automatically refresh the screen when the license boolean (shown in the code above) gets updated. I thought using the @Published property wrapper would do this, but I am needing to restart the app in order to see the change take place. Any ideas?

3      

I would not do this in the WindowGroup just have one View

Have a Bool that check if user has license then pass it to the that View which then decide which View to display

var body: some Scene {
        WindowGroup {
            ContentView(hasLicense: $hasLicense)
        }
    }

Then the ContentView checks to see a GameView or LockedView.

I am assuming that on LockedView you have a way that user can get a license then you change the @Binding var hasLicense to true to show the GameView

Hope that is clear

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.