userId 来 appdelegate 还是不去 Mainviewcontroller 为啥?迅速
Posted
技术标签:
【中文标题】userId 来 appdelegate 还是不去 Mainviewcontroller 为啥?迅速【英文标题】:userId coming to appdelegate still not going to Mainviewcontroller why? in swiftuserId 来 appdelegate 还是不去 Mainviewcontroller 为什么?迅速 【发布时间】:2020-06-05 10:44:52 【问题描述】:NavigationController是isinitial viewcontroller in storyboard
,NavigationController嵌入到LoginViewcontroller
在项目故事板导航中将如下所示
NavigationController->LoginViewcontroller-> RegistrationViewcontroller -> MainViewcontroller
在 PhNUmber 成功注册后,我得到了 userId
,我存储在 KeychainWrapper 中
在 RegistrationViewcontroller 中: 我正在存储 userId,如下所示:
let userID: String=jsonObj?["userId"] as? String ?? ""
KeychainWrapper.standard.set(userID, forKey: "USERID")
我正在 appdelegate 中检查,如下所示进入 Mainviewcontroller:
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
return true
这里savedUserId
来了,为什么我不去MainViewcontroller,一直出现LoginViewcontroller
【问题讨论】:
【参考方案1】:您需要初始化window
。在上面设置rootViewController
:
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
return true
更新:如果你使用SceneDelegate
,你应该使用这个:
class SceneDelegate: UIResponder, UIWindowSceneDelegate
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
guard let windowScene = scene as? UIWindowScene else return
window = UIWindow(windowScene: windowScene)print("appdelegate userid \(savedUserId)")
if savedUserId != nil
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
【讨论】:
如果我像这样var window = UIWindow()
给出错误:“窗口”类型与协议“UIApplicationDelegate”要求的可选性不同
var 窗口:UIWindow?并在didFinishLaunchingWithOptions
in window = UIWindow() window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")............然后还会出现登录视图控制器
我怀疑您是否按照我的预期提供了答案。所以我已经更新了我对你提供的整个代码的答案。
两个用户 ID 都来自 appdelegate userid Optional("415c6073d45a481f991cb81b4c81bf81") saveuserid Optional("415c6073d45a481f991cb81b4c81bf81")。但是 loginviewcontroller 来了
您能检查一下mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController") != nil
是否正在获取true
。【参考方案2】:
试试这个扩展
// add this function in your AppDelegate
func makeRootVC(_ storyBoardName : String, _ vcName : String)
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: self.window!, duration: duration, options: options, animations: , completion: nil)
然后在你的didFinishLaunchingWithOptions
中使用它
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil
print("saveuserid \(savedUserId)")
self.makeRootVC("Main","MainViewController")
return true
【讨论】:
UIView.transition(带有:self.window!,持续时间:持续时间,选项:选项,动画:,完成:无)。错误:线程 1:致命错误:在展开可选值时意外发现 nil @iosApp 你也在用SceneDelegate
吗?
是的 SceneDelegate 存在,但在同一行同样的错误
@iOSApp 参考这里设置 rootViewController 从 sceneDelegate ***.com/questions/58188296/…【参考方案3】:
检查window是否为nil并在AppDelegate中设置rootViewController后添加makeKeyAndVisible()
if(window == nil)
self.window = UIWindow(frame:UIScreen.main.bounds)
if savedUserId != nil
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
self.window?.makeKeyAndVisible()
【讨论】:
如果我在if(window == nil)
中打印用户 ID 两个都打印但不去 mainviewcontroller以上是关于userId 来 appdelegate 还是不去 Mainviewcontroller 为啥?迅速的主要内容,如果未能解决你的问题,请参考以下文章
如何在 xib 的 appdelegate 中检查设备是 ipad 还是 iphone?