iOS 13:无法有条件地从 AppDelegate.swift 导航到不同的 viewontroller



>我在情节提要中将登录视图控制器设置为初始控制器。但是,我想在用户具有活动会话时导航到应用程序的主视图控制器。这是我所拥有的:

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let sessionToken = "abcedef"
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if  sessionToken != nil {
let tabBarVC = storyboard.instantiateViewController(identifier: "TabBarVC")
tabBarVC.modalPresentationStyle = .fullScreen
self.window?.rootViewController?.present(tabBarVC, animated: true, completion: nil)
}
return true
}

但我仍然会看到登录页面。知道我可能做错了什么吗?

InSceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, 
options connectionOptions: UIScene.ConnectionOptions) {
let windowScene = UIWindowScene(session: session, connectionOptions: connectionOptions)
self.window = UIWindow(windowScene: windowScene)
let sessionToken = "abcedef"
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if sessionToken != nil {
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "InitialController") as! InitialController
let rootNC = UINavigationController(rootViewController: vc) // As per need
rootNC.isNavigationBarHidden = true
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}
guard let _ = (scene as? UIWindowScene) else { return }
}

低于 iOS 13AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.initialRootVC()
return true
}
func initialRootVC() {
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "InitialController") as! InitialController
let rootNC = UINavigationController(rootViewController: vc). // when require
rootNC.isNavigationBarHidden = true
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}

对于 iOS 13SceneDelegate.swift,您可以添加以下代码

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
self.initiateRootViewController()
guard let _ = (scene as? UIWindowScene) else { return }
}
func initiateRootViewControlle() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewController = story.instantiateViewController(withIdentifier: "ViewController")
let windowscene = UIApplication.shared.connectedScenes.first
if let windowScene = windowscene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
self.window?.frame = UIScreen.main.bounds
self.window?.windowLevel = UIWindow.Level.statusBar + 1
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}
}

在iOS 13下方添加以下代码AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.initRootController()
return true
}
func initiateRootViewControlle() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewController = story.instantiateViewController(withIdentifier: "ViewController")
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = viewController
self.window?.windowLevel = UIWindow.Level.normal
self.window?.makeKeyAndVisible()
}

我已经测试了这段代码。

class AppDelegate: UIResponder, UIApplicationDelegate
{
var window : UIWindow?
static var sharedDelegate = AppDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppDelegate.sharedDelegate = self
window?.rootViewController = initialViewController()
return true
}
private func initialViewController() -> UIViewController {
let sessionToken = "abcd"
if sessionToken.isEmpty {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
} else {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
}
}
}

这是对我有用的最终解决方案:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Override point for customization after application launch.
let story = UIStoryboard(name: "Main", bundle:nil)
let sessionToken =  "abcedef"
if sessionToken == nil || sessionToken!.isEmpty {
let welcomeVC = story.instantiateViewController(withIdentifier: "WelcomeVC") as? WelcomeViewController
self.window?.rootViewController = welcomeVC
} else {
let tbc = story.instantiateViewController(withIdentifier: "TabBarVC") as? TabBarController
self.window?.rootViewController = tbc
}
guard let _ = (scene as? UIWindowScene) else { return }
}

最新更新