2017-05-24 128 views
1

我试图检查用户是否已成功登录。如果这是真的,那么不会关闭登录屏幕。 TerminView将被加载。 enter image description here打开第二个视图

这里是在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

用户登录状态你为什么不使用NSUserDefaults的保存,如果用户登录? –

+0

调用checkIfLoggedIn in viewDidLoad登录视图控制器 –

+0

保存用户在UserDefaults中的登录状态并检入应用程序委托applicationDidFinishLaunching如果用户登录将根控制器设置为TerminViewController – Maddy

回答

1

试试这个

您需要更改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 
} 
1

如果你想在应用委托类中处理这个,比基于登录状态。您需要设置导航控制器的根视图。我想为更好的方法来处理这种情况后,第一次sucessfull登录你需要存储结果在NSUserdefault。第二次检查这个userdefault并相应地设置你的导航控制器的根视图。

+0

感谢您的快速回答。我会尝试寻找解决方案。 –

1

给loginViewController的实例的appdelegate 使用这的appDelegate

let loginVC: LoginViewController? 

这个在viewDidLoad中LoginViewController

let appDele = UIApplication.shared.delegate as! AppDelegate 
    appDele.loginVC = self 

的,这在应用并完成发射

func applicationDidFinishLaunching(_ application: UIApplication) { 

    if(loginVc != nil){ 
    loginVC.checkIfLoggedIn() }} 

它建议使用userdefaults但如果你想继续使用该解决方案,然后尝试这个

1

在您的应用程序委托中,您必须设置根控制器。根据你的要求见下面的代码。

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. 
} 
相关问题