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

时间:2021-01-03 21:33:53

标签: swiftui in-app-purchase property-wrapper

我的应用程序需要许可证才能正常使用,因此当应用程序加载时,我会检查他们是否有许可证,如果有,他们会转到主 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 属性包装器可以做到这一点,但我需要重新启动应用程序才能看到发生的变化。有什么想法吗?

0 个答案:

没有答案
相关问题