0

// 我正在尝试使用@Binding 将@AppStorage 的值传递给类。// @AppStorge 的声明,这是唯一的事实来源。// 我在 SigninViewModel = Class 'SigninViewModel' has no initializers 上收到以下错误

    import SwiftUI
    
    struct MainView: View {
        @AppStorage("userSignIn") var userSignIn = false
    
        var body: some View {
            ZStack {
                TabView {
                    AppointmentsView()
                        .tabItem {
                            Image(systemName: "calendar")
                            Text("Appointments")
                        }
                    
                    AccountView(userSignIn: .constant(true))
                        .tabItem {
                            Image(systemName: "person")
                            Text("Profile")
                        }
                    
                    StatsView()
                        .tabItem {
                            Image(systemName: "chart.bar")
                            Text("Stats")
                        }
                }
            }
        }
    }
    
    

// 这个带有@Binding 的类正在被声明,并且我试图在其中访问@AppStorae 值。

    class SigninViewModel: ObservableObject {
        @Published var nonce = ""
        @Binding var userSignIn: Bool
    
        
        func authenticate(credential: ASAuthorizationAppleIDCredential) {
            
            // getting Token...
            guard let token = credential.identityToken else {
                print("Error with Firebase")
                return
            }
            
            //Token String...
            guard let tokenString = String(data: token, encoding: .utf8) else {
                print("Error with Token")
                return
            }
            
            let firebaseCredential = OAuthProvider.credential(withProviderID: "apple.com",
                                                              idToken: tokenString,
                                                              rawNonce: nonce)
            
            Auth.auth().signIn(with: firebaseCredential) { (result, err) in
                if let error = err {
                    print(error.localizedDescription)
                    return
                }
                
                // User succesfully logged into Firebase...
                print("Logged in Success")
                            
                
                // Directing user to Main page...
                withAnimation(.easeInOut) {
                    self.userSignIn = true
                }
            }
        }
    }
4

1 回答 1

0

你可以把@AppStorage("userSignIn")里面的ObservableObject,但是你的代码很快就变得一团糟。

我认为更简单的结构是将所有身份验证状态放在一个单一的内部,AuthenticationModel: ObservableObject并将其用作@StateObject. MainView此外,我建议将其推到视图树上。

class AuthenticationModel: ObservableObject {
    @Published var nonce = ""
    @Published var userSignIn: Bool

    init() {
        let defaults = UserDefaults.standard
        userSignIn = defaults.bool(forKey: "userSignIn")
    }

    func authenticate(credential: ASAuthorizationAppleIDCredential)
        ...

        // Successful Login
        let defaults = UserDefaults.standard
        defaults.set(true, forKey: "userSignIn")
        
        // Update the views
        userSignIn = true
    }
}


struct MainView: View {
    @StateObject var authModel: AuthenticationModel = AuthenticationModel()

    var body: some View {
        if authModel.userSignIn {
            Home(authModel: authModel)
        } else {
            SignIn(authModel: authModel)
        }
    }
}


struct SignIn: View {
    @ObservedObject var authModel: AuthenticationModel

    var body: some View {
        Button("Sign In") {
            authModel.authenticate(...)
        }
    }
}
于 2022-01-09T17:51:26.043 回答