我尝试检查用户是否已成功登录。如果这是真的,则不会加载登录屏幕。将加载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()
}
我的错误在哪里?
答案 0 :(得分:1)
如果你想在app delegate类中处理这个,而不是基于登录状态。您需要设置导航控制器的根视图。我认为在第一次成功登录后需要更好的方法来处理这种情况你需要将结果存储在NSUserdefault中。第二次检查此用户默认值并相应地设置导航控制器的根视图。
答案 1 :(得分:1)
将loginViewController的实例提供给appdelegate 在appDelegate中使用它
let loginVC: LoginViewController?
这在LoginViewController的viewDidload中
let appDele = UIApplication.shared.delegate as! AppDelegate
appDele.loginVC = self
这在应用程序中完成了启动
func applicationDidFinishLaunching(_ application: UIApplication) {
if(loginVc != nil){
loginVC.checkIfLoggedIn() }}
建议使用userdefaults,但如果您想继续使用此解决方案,请尝试使用
答案 2 :(得分:1)
试试这个
您需要使用登录标志
更改rootViewControllerfunc 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
}
答案 3 :(得分:1)
在您的app委托中,您必须设置根控制器。根据您的要求,请参见下面的代码。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Main your storyboard identifier
let mainStoryBoard = UIStoryboard(name: "Your_Storyboard_identifier", bundle: nil)
var rootController:UIViewController
//Retrieve Login Status if user is logged in or not
let loginStatus: Bool = UserDefaults.standard.bool(forKey: "LoginStatus")
// if login status is true show TerminViewController else LoginViewController
if loginStatus == true {
//Your TerminViewController
rootController = mainStoryBoard.instantiateViewController(withIdentifier: "TerminViewController")
}
else{
//Your LoginViewController
rootController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController")
}
//setting root view controller
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = rootController
return true
}
在登录按钮上单击保存UserDefaults
@IBAction func login(_ sender: Any) {
//Store user login status in userdefaults and check it in app delegate when the user open your app again.
UserDefaults.standard.set(true, forKey: "LoginStatus")
UserDefaults.standard.synchronize()
//Your Logic here ..
// On user login you might be navigating to TermViewController.
}