用户登录后,我试图跳过我的登录视图。如何在应用程序启动时检查用户是否通过Facebook登录?
我当前在LoginViewController中有以下代码:

override func viewWillAppear(animated: Bool) {
    var loggedIn = PFFacebookUtils.session().isOpen;

    if (loggedIn) {
        performSegueWithIdentifier("skipLogin", sender: self)
    }

}

即使用户点击了“使用Facebook登录”按钮,这也不会移动到我的下一个视图。
我得到以下错误:
警告:尝试继续打开
其视图不在窗口层次结构中!

最佳答案

如chat中所述,您基本上有两个选项:
让用户“看到”从登录视图控制器到第二个控制器的动画。在这种情况下,您应该执行push-InviewDidAppear而不是viewWillAppear(在视图没有完全准备好的情况下,正如运行时警告明确指出的那样)。
如果您希望在没有任何动画的情况下立即显示最终视图控制器,那么最好将该逻辑放在应用程序委托中,并从这里选择应加载的初始视图控制器。在这种情况下,实际上并没有执行任何segue,只是将一个或另一个视图控制器分配给主窗口(或导航控制器)。
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
}

在这两种方法中,它们都使用以下框架:
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)
}

present...ViewControllernavigationController变量的定义如下:
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var navigationController: UINavigationController?
    ...
}

如果你的应用程序也使用导航控制器作为其根视图控制器,你可能可以使用相同的代码。

关于ios - 解析Facebook用户登录,执行segue,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28616819/

10-10 21:07