xcode 检查用户是否已登录 ios swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37920021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:56:26  来源:igfitidea点击:

check if user is logged in ios swift

iosiphonexcodeswiftswift2

提问by kvra13

i want check in appdelegate.swift file if user logged in showing HomeviewController like Instagram and if not Showing LoginViewController , iam using mysql to save users data and php bridge lang

我想检查 appdelegate.swift 文件,如果用户登录显示 HomeviewController 像 Instagram,如果没有显示 LoginViewController ,我使用 mysql 来保存用户数据和 php 桥接语言

code in appdalegate.swift

appdalegate.swift 中的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

         var rootViewController = self.window!.rootViewController
         let isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
         if(isUserLoggedIn) {
         let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
         let protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("goToHome") as! HomeViewController
         window!.rootViewController = protectedPage
         window!.makeKeyAndVisible()
         }
         else{
         let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
         let loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginview") as! LoginViewController
         window!.rootViewController = loginViewController
         window!.makeKeyAndVisible()


         }


        return true
    }

loginviewcontoler.swift

loginviewcontoler.swift

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

                        //[jsonData[@"success"] integerValue];

                        NSLog("Success: %ld", success);

                        if(success == 1)
                        {
                            NSLog("Login SUCCESS");

                            let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
                            prefs.setObject(username, forKey: "USERNAME")
                            prefs.setInteger(1, forKey: "ISLOGGEDIN")
                            prefs.synchronize()


                            self.performSegueWithIdentifier("goToHome", sender: self)

ProfileViewController.swift

ProfileViewController.swift

 override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)


        let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
        let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int


        if (isLoggedIn != 1) {

            self.performSegueWithIdentifier("goto_login", sender: self)

        }

        else
        {

            self.usernameLabel.text = prefs.valueForKey("USERNAME") as? String

        }

    }

sorry for long question

抱歉问了很长的问题

回答by Alexander B

Don't use in this situation NSUserDefaults

在这种情况下不要使用 NSUserDefaults

https://github.com/matthewpalmer/Locksmith

https://github.com/matthewpalmer/Locksmith

Here is good example of using safe LogIn / LogOut so, after implementation

这是使用安全登录/注销的好例子,因此,在实施后

if let dictionary = Locksmith.loadDataForUserAccount("accaunt")
{
//go to profile
}
 else
{
//go to login
}

回答by Mitul Marsoniya

Follow step:-

按照步骤:-

Step 1 :- Create custom SplashViewControllerset rootViewController SplashViewController
Step 2 :- Write login service call on SplashViewController
Setp 3 :- When you get Login success then just change rootViewController
Setp 4 :- Write one method in AppDelegateFor change rootViewControllerfor redirect Homescreen.
Setp 5 :- Call AppDelegatemethod for go to Homescreen.

第 1 步:- 创建custom SplashViewControllerset rootViewController SplashViewController
第 2 步:- 在SplashViewController
Setp 3上编写登录服务调用:- 当您登录成功时,只需更改rootViewController
Setp 4:- 在AppDelegateFor change rootViewControllerfor redirect 中编写一个方法Homescreen
第 5 步:- 调用AppDelegatego to 方法Homescreen

回答by Mashqur

You have to do some works. 1. You have to make sure that the user is logged in. to do that when you press the button (login button) use UserDefaults

你必须做一些工作。1. 您必须确保用户已登录。当您按下按钮(登录按钮)时,请使用 UserDefaults

let userDefault = UserDefaults.standard
userDefault.set(true, forKey: "isLoggedIn")
userDefault.synchronize()

2.When user will launch the app again just check whether the user logged in or not in viewDidAppear method/ viewDidLoad(its your choice which method you will use. most of the time I use viewDidAppear)

2.当用户再次启动应用程序时,只需在viewDidAppear方法/viewDidLoad中检查用户是否登录(由您选择使用哪种方法。大部分时间我使用viewDidAppear)

let userDefault = UserDefaults.standard

let savedData = userDefault.bool(forKey: "isLoggedIn")
if(savedData){
  performSegue(withIdentifier: "segueTest", sender:nil)//here u have decide the which view will show if the user is logged in how. here i used   segue.

}else{
   viewController = self// this is the main view. just make the object of the class and called it.
}

I hope you know how to prepare segue

我希望你知道如何准备 segue

回答by Nouru Muneza

Just a concise way of doing the same thing:

只是做同样事情的简洁方式:

func checkIfUserIsLoggedIn() {
    if Auth.auth().currentUser == nil {
        DispatchQueue.main.async {
            // present login controller
            let loginVC = LoginVC()
            let navController = UINavigationController(rootViewController: loginVC)
            self.present(navController, animated: true, completion: nil)
        }
        return
    }
}

Upvote if this was helpful :)

如果这有帮助,请点赞 :)