Ios 如何使用Swift更改LaunchScreen中的初始ViewController?

Ios 如何使用Swift更改LaunchScreen中的初始ViewController?,ios,swift,viewcontroller,Ios,Swift,Viewcontroller,我想检查用户是否已登录,如果用户已登录,则将其带到主屏幕,或显示欢迎屏幕。您不能在启动屏幕中执行此操作,但您可以在AppDelegate的方法didFinishLauchingWithOption中执行相同操作,在那里,您可以检查用户是否登录,并在情节提要中设置根视图控制器和不设置initialViewController。 应该是这样的 NSString *identifier = isLoggedIn ? @"IdentifierForVC1" : @"IdentifierForVC

我想检查用户是否已登录,如果用户已登录,则将其带到主屏幕,或显示欢迎屏幕。

您不能在启动屏幕中执行此操作,但您可以在AppDelegate的方法
didFinishLauchingWithOption中执行相同操作,在那里,您可以检查用户是否登录,并在情节提要中设置根视图控制器和不设置initialViewController。
应该是这样的

    NSString *identifier = isLoggedIn ? @"IdentifierForVC1" : @"IdentifierForVC2";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier: identifier];
    self.window.rootViewController = vc;
未在编辑器中测试的代码可能有一些错误 Swift代码应该是这样的

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
self.window?.rootViewController = vc

在Swift中,在AppDelegate.Swift中

self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("{INSERT_STORYBOARD_ID_HERE}") as? UIViewController
Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let viewController = storyboard.instantiateViewController(withIdentifier: "Identifier")
   let navigationController = UINavigationController(rootViewController: viewController)
   self.window?.rootViewController = navigationController
   self.window?.makeKeyAndVisible()

   return true
}

您不能在启动屏幕中执行,但可以在AppDelegate中执行

斯威夫特4号

 if userLoggedIn == True {
        //Do something 
        } else {
        //Do something
        }
        UserDefaults.standard.set(value:Bool  ,forKey:"loggedIn")  //This will save the bool value to your UserDefaults
      }


现在转到AppDelegate



快乐编码
希望这有帮助:)

在AppDelegate Swift 4.0中编写此代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let windowScene = scene as? UIWindowScene{

            let window = UIWindow(windowScene: windowScene)
            let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController

            window.rootViewController = rootViewController
            self.window = window
            window.makeKeyAndVisible()
        }

    } 

didfishlaunchwithoptions中
将viewController传递给
self.launch(rootController:viewController())


在AppDelegate Swift 5.0 Xcode 11.0中编写此代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let windowScene = scene as? UIWindowScene{

            let window = UIWindow(windowScene: windowScene)
            let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController

            window.rootViewController = rootViewController
            self.window = window
            window.makeKeyAndVisible()
        }

    } 


你能给出一些代码示例吗?非常感谢你!我是说Swift密码。我真的不知道如何写objective-c代码它工作!!!谢谢!!!(我将行
self.windows?
改为
self.window
,我认为这是swift 2.0的新语法,因为它说self没有一个名为
windows
)的属性不,它不是语法我这边的拼写错误,应该是windo
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let _ = (scene as? UIWindowScene) else { return }

        if let windowScene = scene as? UIWindowScene{

            let window = UIWindow(windowScene: windowScene)
            let rootViewController = UIStoryboard(name: "Auth", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! UINavigationController

            window.rootViewController = rootViewController
            self.window = window
            window.makeKeyAndVisible()
        }

    }