Parse Facebook用户登录后执行segue

时间:2015-02-19 21:02:55

标签: ios xcode facebook swift parse-platform

我试图在用户登录后跳过我的登录视图。如何在应用程序启动时检查用户是否通过Facebook登录?

我目前在 LoginViewController

中有以下代码
override func viewWillAppear(animated: Bool) {
    var loggedIn = PFFacebookUtils.session().isOpen;

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

}

enter image description here

即使在用户点击了"使用Facebook登录后,这也不会转移到我的下一个视图"按钮。

我收到以下错误:

  

警告:尝试persent< _Project.HomeViewController:0x7fa331d3af00>上   < _Project.LoginViewController:0x7fa331f08950>其视图不在窗口层次结构中!

1 个答案:

答案 0 :(得分:4)

正如聊天中所讨论的,这里基本上有两个选项:

  • 让用户"看"从登录视图控制器到第二个动画的动画。在这种情况下,你应该在viewDidAppear而不是viewWillAppear中进行推送(视图没有完全准备好,因为运行时警告明确说明了这一点。)
  • 如果您希望立即显示最终视图控制器而不进行任何动画,那么最好将该逻辑放入您的app委托中,并选择应从此处加载哪个初始视图控制器。在这种情况下,您实际上并未执行任何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
}

在两种方法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)
}

navigationControllerwindow变量应在AppDelegate中定义如下:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var navigationController: UINavigationController?
    ...
}

如果您的应用还使用导航控制器作为其根视图控制器,则可以使用相同的代码。