【发布时间】:2023-01-09 03:49:27
【问题描述】:
在我的 SwiftUI 应用程序中,我合并了一个登录页面。我添加了一个注销按钮,它确实会将您带回到登录页面,但是,一旦我重新打开应用程序,它就不会保持注销状态。
下面是登录页面所在的位置。
**class AppViewModel:ObservableObject{
**
let auth = Auth.auth()
@Published var signedIn = false
var isSignedIn: Bool {
return auth.currentUser != nil //not signed in
}
func signIn(email: String, password: String){
auth.signIn(withEmail: email,
password: password){ [weak self] result, error in
guard result != nil, error == nil else{
return
}
DispatchQueue.main.async{
//Success
self?.signedIn = true
}
}
}
func signUp(email: String, password: String){
auth.createUser(withEmail: email, password: password){[weak self] result, error in
guard result != nil, error == nil else{
return
}
//success
DispatchQueue.main.async{
//Success
self?.signedIn = true
}
}
}
func signOut(){
try? auth.signOut()
self.signedIn = false
}
}
**struct ContentView: View {
** @EnvironmentObject var viewModel: AppViewModel
var body: some View {
NavigationView{
if viewModel.signedIn {
MainPage()
}
else{
}
}
.onAppear{
viewModel.signedIn = viewModel.isSignedIn
}
}
}
struct SignInView: View {
@State var email = ""
@State var password = ""
@EnvironmentObject var viewModel: AppViewModel
var body: some View {
VStack{
Image("crest")
.resizable()
.scaledToFit()
VStack{
TextField("Email Address", text: $email)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
SecureField("Password", text: $password)
.padding()
.background(Color(.secondarySystemBackground))
Button(action: {
guard !email.isEmpty, !password.isEmpty else{
return
}
viewModel.signIn(email: email, password: password)
}, label: {
Text("Sign In")
.foregroundColor(Color.white)
.frame(width: 200, height: 50)
.cornerRadius(8)
.background(Color.blue)
})
NavigationLink("Create Account", destination: SignUpView())
}
.padding()
Spacer()
}
.navigationTitle("Sign In")
}
}
struct SignUpView: View {
@State var email = ""
@State var password = ""
@EnvironmentObject var viewModel: AppViewModel
var body: some View {
VStack{
Image("crest")
.resizable()
.scaledToFit()
VStack{
TextField("Email Address", text: $email)
.disableAutocorrection(true)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
SecureField("Password", text: $password)
.padding()
.background(Color(.secondarySystemBackground))
Button(action: {
guard !email.isEmpty, !password.isEmpty else{
return
}
viewModel.signUp(email: email, password: password)
}, label: {
Text("Create Account")
.foregroundColor(Color.white)
.frame(width: 200, height: 50)
.cornerRadius(8)
.background(Color.blue)
})
}
.padding()
Spacer()
}
.navigationTitle("Create Account")
}
}
然后在下面是我加入注销按钮的地方(在不同的视图中)
@EnvironmentObject var appViewModel: AppViewModel
....
Button {
appViewModel.signedIn = false
} label: {
Text("Sign Out")
}
这会将当前视图重新加载到登录页面,但不会停留在那里。
提前感谢您的帮助。
尝试弄乱 contentView 以了解为什么“signedIn”变量没有保留并围绕它进行研究。
【问题讨论】:
-
您正在覆盖并消除从 firebase 获取错误以查看发生了什么的任何可能性。查看授权文档 done use
try?usedo try catch并使用提供的侦听器来确定用户是否真正登录。您现在的编码方式FirebaseAuth毫无用处。
标签: swift swiftui firebase-authentication