我尝试检查用户是否已成功登录。如果为true,则不加载登录屏幕。 TerminView将被加载。
这是LoginViewController中的函数(我也想使用此函数,当用户在按下“登录”按钮后成功登录时)
func checkIfLoggedIn() -> Void {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.appendingPathComponent(K.login.file)?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath!) {
self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "TerminView") as UIViewController, animated: true)
}
}
在情节提要中,我还给TerminViewController提供了ID:TerminView
这是AppDelegate中此函数的调用:
func applicationDidFinishLaunching(_ application: UIApplication) {
LoginViewController().checkIfLoggedIn()
}
我的错在哪里?
最佳答案
尝试这个
您需要使用登录标志更改rootViewController
func isUserLoggedIN() -> Bool {
var struserID: String? = ""
if UserDefaults.standard.object(forKey:"LoginKey") != nil {
struserID = UserDefaults.standard.object(forKey:"LoginKey") as! String?
}
return struserID!.characters.count > 0 ? true : false
}
如下检查
if self.isUserLoggedIN() {
let navLog: UINavigationController? = Constant.StoryBoard.instantiateViewController(withIdentifier: "loginNavBar") as? UINavigationController
window?.rootViewController = navLog
}
else{
// set another ViewController
}