我正在开发我的第一个应用程序,我无法理解如何在用户成功登录后使用NSUserDefaults
更改我的初始视图控制器。(一旦用户成功登录,用户将不再必须在启动应用程序后登录)我已阅读其他帖子和Google搜索教程,但老实说我无法理解它们。
我主要是格式问题。
修改
以下是我尝试添加的新代码:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
if PFUser.currentUser() != nil {
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Timeline") as! UIViewController
}else {
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
但是,每次我尝试启动应用程序时,它都会立即崩溃。错误说明:
2015-08-19 10:30:23.966 Venddy[3451:95841] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
如果有人能给我建议,我将不胜感激。我是应用程序开发的新手。 谢谢!
答案 0 :(得分:4)
您应该在AppDelegate.swift
方法内的didFinishLaunchingWithOptions
文件中进行设置。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//SET INITIAL CONTROLLER
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController: UIViewController
let userDefaults = NSUserDefaults.standardUserDefaults()
if let login = userDefaults.objectForKey("loginSuccess") as! Bool //your condition if user is already logged in or not
{
// if already logged in then redirect to MainViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController // 'MainController' is the storyboard id of MainViewController
}
else
{
//If not logged in then show LoginViewController
initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginController") as! LoginViewController // 'LoginController' is the storyboard id of LoginViewController
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
希望这会有所帮助!
答案 1 :(得分:0)
你可以这样做
您只需将LoginSignup视图控制器添加到故事板,然后将“Storyboard Id”设置为“loginVC”。之后,在application:didFinishLaunchingWithOptions中检查用户是否登录。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let logined = ?
var vc: UIViewController?
if !logined {
vc = storyboard.instantiateViewControllerWithIdentifier("loginVC")
} else {
vc = storyboard.instantiateInitialViewController()
}
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
不要忘记在“部署信息”中的目标设置中删除“主界面”的值
答案 2 :(得分:0)
用于Swift 4.2和5代码的代码:
self.window = UIWindow(frame: UIScreen.main.bounds)
if(loggedIn){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "dashboardVC")
} else {
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "loginVC")
}
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()