微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据变量更改初始屏幕 - SwiftUI

如何解决根据变量更改初始屏幕 - SwiftUI

我的应用程序需要许可证才能正常使用,因此当应用程序加载时,我会检查他们是否有许可证,如果有,他们会转到主 ContentView,如果没有,请转到 { {1}}。我正在使用购买框架来管理应用内购买。

LockedView

在初始化程序中,我运行 @main struct RugbyAppApp: App { @Observedobject var userSettings = UserSettings.shared var swiftUISpeech = SwiftUISpeech() init(){ Purchases.debugLogsEnabled = true Purchases.configure(withAPIKey: "xxx") userSettings.checkSubscription() } var body: some Scene { WindowGroup { if(userSettings.license == true){ ContentView().id(appState.gameID) } else{ LockedView() } } } } 这是这样的:

userSettings.checkSubscription()

如果交易完成,并且购买框架收到消息说一切都已经完成,我只需像这样更新许可证:

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
            }
        }
    }
}

我希望应用程序在许可证布尔值(如上面的代码所示)更新时自动刷新屏幕。我认为使用 userSettings.license = true 属性包装器可以做到这一点,但我需要重新启动应用程序才能看到发生的变化。有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。