我想实现firebase键:安全检查,
值:“admin”进入视图“adminVC”,值:“ON”进入视图“MainTabBarController”,
没有这个键:安全检查查看“SignUpViewControllerID”
以下是我当前的代码,但它不起作用,希望有人能给我一些建议,谢谢
Database.database().reference().child("ID/\(self.uid)/Profile/Safety-Check").observeSingleEvent(of: .value, with: { (snapshot) in let cheak = "Safety-Check" if cheak == "admin" { if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AdminVC") { GetWindow()?.rootViewController = viewController GetWindow()?.makeKeyAndVisible() } if cheak == "ON" { if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") { GetWindow()?.rootViewController = viewController GetWindow()?.makeKeyAndVisible() }else{ if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SignUpViewControllerID") { GetWindow()?.rootViewController = viewController GetWindow()?.makeKeyAndVisible()
[已解决]问题 2. 黄色警告:“keyWindow”在 iOS 13.0 中已弃用:不应用于支持多个场景的应用程序,因为它会在所有连接的场景中返回一个关键窗口。
我应该如何修改它来解决这个问题?
UIApplication.shared.keyWindow?.rootViewController = viewController
使用以下函数获取窗口:
func GetWindow() -> UIWindow? { if #available(iOS 13.0, *) { let sceneDelegate = UIApplication.shared.connectedScenes .first?.delegate as? SceneDelegate let appDelegate = UIApplication.shared.delegate as? AppDelegate return sceneDelegate?.window ?? appDelegate?.window } else { let appDelegate = UIApplication.shared.delegate as? AppDelegate return appDelegate?.window } }
上述函数的用法:
if let window = GetWindow() {}
关于您的情况:
GetWindow()?.rootViewController = viewController
所以替换这些行:
UIApplication.shared.keyWindow?.rootViewController = viewController self.dismiss(animated: true, completion: nil)
和 :
GetWindow()?.rootViewController = viewController GetWindow()?.makeKeyAndVisible()
而且您无需关闭,因为它使用此代码设置了一个新的根视图控制器。