我试图在用户登录后跳过我的登录视图。如何在应用程序启动时检查用户是否通过Facebook登录?
我目前在 LoginViewController :
中有以下代码override func viewWillAppear(animated: Bool) {
var loggedIn = PFFacebookUtils.session().isOpen;
if (loggedIn) {
performSegueWithIdentifier("skipLogin", sender: self)
}
}
即使在用户点击了"使用Facebook登录后,这也不会转移到我的下一个视图"按钮。
我收到以下错误:
警告:尝试persent< _Project.HomeViewController:0x7fa331d3af00>上 < _Project.LoginViewController:0x7fa331f08950>其视图不在窗口层次结构中!
答案 0 :(得分:4)
正如聊天中所讨论的,这里基本上有两个选项:
viewDidAppear
而不是viewWillAppear
中进行推送(视图没有完全准备好,因为运行时警告明确说明了这一点。)Parse有" AnyWall"实现第二个逻辑的示例应用程序。有关详细信息,请参阅此处:https://parse.com/tutorials/anywall#2-user-management。特别是,第2.4章是特别感兴趣的,因为它解释了如何保持用户登录。
简单地说,他们是如何做到的(我已经将他们的Objective-C代码改编为Swift):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
navigationController = UINavigationController()
...
// If we have a cached user, we'll get it back here
if PFFacebookUtils.session().isOpen {
// A user was cached, so skip straight to the main view
presentWallViewController(animated: false)
} else {
// No cached user, go to the welcome screen and
// have them log in or create an account.
presentLoginViewController(animated: true)
}
...
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = navigationController
window.makeKeyAndVisible()
return true
}
在两种方法present...ViewController
的每一种中,它们使用以下骨架:
func presentxxxViewController(#animated: Bool) {
NSLog("Presenting xxx view controller")
// Go to the welcome screen and have them log in or create an account.
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Here you need to replace "Main" by the name of your storyboard as defined in interface designer
let viewController = storyboard.instantiateViewControllerWithIdentifier("xxx") as xxxViewController // Same here, replace "xxx" by the exact name of the view controller as defined in interface designer
//viewController.delegate = self
navigationController?.setViewControllers([viewController], animated: animated)
}
navigationController
和window
变量应在AppDelegate
中定义如下:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
...
}
如果您的应用还使用导航控制器作为其根视图控制器,则可以使用相同的代码。