用户登录后,在登录视图和仪表板视图之间切换不起作用


let storyboard = UIStoryboard(name: "Main", bundle: nil)


let dashboard = storyboard.instantiateViewController(identifier: "DashboardViewController")
(UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.changeRootViewController(_vc: dashboard)
}

UIApplication.shared.delegateappDelegate?。窗rootViewController=仪表板

类SceneDelegate:UIResponder,UIWindowSceneDeligate{

var window: UIWindow?

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).
guard let _ = (scene as? UIWindowScene) else { return }

let storyboard = UIStoryboard(name: "Main", bundle: nil)

if let loggedUsername = UserDefaults.standard.string(forKey: "user"){

let dashboard = storyboard.instantiateViewController(identifier: "DashboardViewController")
window?.rootViewController = dashboard
}
else{
let loginView = storyboard.instantiateViewController(identifier: "ViewController")
window?.rootViewController = loginView
}




}

我也试过这个,但它没有起作用太

func changeRootViewController( _vc: UIViewController, animted: Bool = true){
guard let window = self.window else {
return
}

UIView.transition(with: window,
duration: 0.5,
options: .transitionCurlDown,
animations: nil,
completion: nil)

}
var window: UIWindow?
var navigationC: UINavigationController?
private(set) static var shared: SceneDelegate?

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).
guard let _ = (scene as? UIWindowScene) else { return }

let storyboard = UIStoryboard(name: "Main", bundle: nil)

if let loggedUsername = UserDefaults.standard.string(forKey: "user"){
let dashboardvc = storyboard.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
self.navigationC = UINavigationController(rootViewController: dashboardvc)
} else{
let loginvc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationC = UINavigationController(rootViewController: loginvc)
}
self.navigationC!.setNavigationBarHidden(true, animated: false)
self.window?.clipsToBounds = true
self.window?.rootViewController = navigationC
self.window?.makeKeyAndVisible()
Self.shared = self
}

class SceneDelegate:UIResponder,UIWindowSceneDeligate{

var window: UIWindow?
var navigationC : UINavigationController?
private(set) static var shared: SceneDelegate?

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).
guard let _ = (scene as? UIWindowScene) else { return }

if UserDefaults.value(forKey: "isLogin") != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let dashboard = storyboard.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
self.navigationC = UINavigationController(rootViewController: dashboard)
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginPage = storyboard.instantiateViewController(withIdentifier: "ViewController") as!ViewController
self.navigationC = UINavigationController(rootViewController: loginPage)
}
self.navigationC!.setNavigationBarHidden(true, animated: false)
self.window?.clipsToBounds = true
self.window?.rootViewController = navigationC
self.window?.makeKeyAndVisible()
Self.shared = self




}
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as?NSDictionary

if let parseJSON = json {
let accessToken = parseJSON["token"] as? String
let username = parseJSON["user"] as? String

_ = UserDefaults(suiteName: username)

print("Access token: (String(accessToken!))")

let saveAccessToken: Bool = KeychainWrapper.standard.set(accessToken!, forKey: "accessToken")

let saveUsername: Bool = KeychainWrapper.standard.set(username!, forKey: "username")

if(accessToken?.isEmpty)! {
self.displayMessage(userMessage: "Inloggen is niet success, probeer later")
return
}
DispatchQueue.main.async {

USERDEFAULT.set(true, forKey: "isLogin")
let dashboard = self.storyboard?.instantiateViewController(identifier: "DashboardViewController") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)
}

}

do{let json=将JSONSerialization.jsonObject(带有:data!,options:.allowFragments(作为?NSDictionary

if let parseJSON = json {
let accessToken = parseJSON["token"] as? String
let username = parseJSON["user"] as? String

_ = UserDefaults(suiteName: username)

print("Access token: (String(accessToken!))")

let saveAccessToken: Bool = KeychainWrapper.standard.set(accessToken!, forKey: "accessToken")

let saveUsername: Bool = KeychainWrapper.standard.set(username!, forKey: "username")

if(accessToken?.isEmpty)! {
self.displayMessage(userMessage: "Inloggen is niet success, probeer later")
return
}
DispatchQueue.main.async {


let defaults = UserDefaults.standard
defaults.set(true, forKey: "isLogin")


let dashboard = self.storyboard?.instantiateViewController(identifier: "DashboardViewController") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)
}

}
}catch{
self.removeActivityIndicator(activityIndicator: myactivit)
self.displayMessage(userMessage: "Probeer later")
return

}
}

\这是我的场景电报文件

类SceneDelegate:UIResponder,UIWindowSceneDeligate{

var window: UIWindow?
var navigationC : UINavigationController?
private(set) static var shared: SceneDelegate?

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).
guard let _ = (scene as? UIWindowScene) else { return }

if UserDefaults.value(forKey: "isLogin") != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let dashboard = storyboard.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
self.navigationC = UINavigationController(rootViewController: dashboard)
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginPage = storyboard.instantiateViewController(withIdentifier: "ViewController") as!ViewController
self.navigationC = UINavigationController(rootViewController: loginPage)
}
self.navigationC!.setNavigationBarHidden(true, animated: false)
self.window?.clipsToBounds = true
self.window?.rootViewController = navigationC
self.window?.makeKeyAndVisible()
Self.shared = self




}
func changeRootViewController( _vc: UIViewController, animted: Bool = true){
guard let window = self.window else {
return
}
UIView.transition(with: window,
duration: 0.5,
options: .transitionCurlDown,
animations: nil,
completion: nil)
}

\我收到这个错误

异常NSException*";[<NSUserDefaults 0x7fff86b8bd58>valueForUndefinedKey:]:此类不符合注册表项isLogin的键值编码"0x0000600001 bc930name __NSCFConstantString*"NSUnknownKeyException";0x00007fff801e7c90原因__NSCFString*";[<NSUserDefaults 0x7fff86b8bd58>valueForUndefinedKey:]:此类不符合注册表项isLogin的键值编码"0x00006000026e8090userInfo __NSDictionaryI*2密钥/值对0x00006000001a4d40保留__NSDictionaryM*2密钥/值对0x00006000014c0ac0

DispatchQueue.main.async {
let dashboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController

self.view.window?.rootViewController = dashboard
self.view.window?.makeKeyAndVisible()                            
}

最新更新