作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我几乎完成了在 Xcode 中开发我的第一个 Swift 应用程序。我正在使用火力基地。我的问题是,在转到每个 View Controller 时,我是否应该检查用户是否仍处于登录状态?
我目前只在两个选项卡 View Controller 上 checkin viewdidload()
方法。我必须在每个页面上都这样做吗?
我正在使用下面的代码来检查用户是否登录到主屏幕,如果他们没有:
FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
// 2
if user != nil {
// 3
self.performSegue(withIdentifier: "fromListToHome", sender: self)
}
}
}
最佳答案
保留 User
类的全局实例总是明智的,您可以在其中存储所有详细信息,在本例中为 static let shared_Instance = user_Global()
class user_Global {
var refresh_Delegate : user_Data_Refresh?
static let shared_Instance = user_Global()
var is_Authenticated : Bool = false{
didSet{
refresh_Delegate?.refresh_User_State()
}
}
}
使那些依赖于用户身份验证状态的 View Controller 符合 user_Data_Refresh
协议(protocol),并检查 user_Global.shared_Instance.is_Authenticated
bool 值
// FIREBASE User Refresh protocol.....
protocol user_Data_Refresh : class {
func refresh_User_State()
}
检查授权状态的函数...
func isUserSignedIn(completion_Block : @escaping (_ user_State : Bool?) -> Void){
Auth.auth().addStateDidChangeListener({ (auth, user) in
if user != nil{
print("The user is Authenticated")
user_Global.shared_Instance.is_Authenticated = true
completion_Block(true)
return
}else{
print("The user is not Authenticated")
user_Global.shared_Instance.is_Authenticated = false
completion_Block(false)
return
}
})
}
在您的 appDelegate 中调用此函数以使此线程在您的网络链接中运行...
关于Ios 开发 : How often should an app check user is still logged in?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461762/
我是一名优秀的程序员,十分优秀!