ios - 解析 Facebook 用户登录,执行 segue

标签 ios xcode facebook swift parse-platform

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

我目前在 LoginViewController 中有以下代码:

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

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

}

enter image description here

即使在用户单击“使用 Facebook 登录”按钮后,这也不会转到我的下一个 View 。

我收到以下错误:

Warning: Attempt to persent <_Project.HomeViewController: 0x7fa331d3af00> on <_Project.LoginViewController: 0x7fa331f08950> whose view is not in the window hierarchy!

最佳答案

正如在聊天中讨论的那样,您在这里基本上有两个选择:

  • 让用户“看到”从登录 View Controller 到第二个 View Controller 的动画。在这种情况下,您应该在 viewDidAppear 而不是 viewWillAppear 中执行推送( View 未完全准备好,正如运行时警告明确指出的那样)。
  • 如果您希望立即显示最终 View Controller ,而不使用任何动画,那么最好将该逻辑放在您的应用委托(delegate)中,并选择应从此处加载哪个初始 View Controller 。在这种情况下,您实际上并没有执行任何 segue,您只是将一个或另一个 View Controller 分配给主窗口(或您的导航 Controller )。

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?
    ...
}

如果您的应用还使用导航 Controller 作为其 Root View Controller ,您可能可以使用相同的代码。

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

相关文章:

iphone - 制作精美的tabBar图标

iOS 企业配置文件到期

ios - 建议在不使用 MacOS 或 Xcode 的情况下在 iOS(最好是 9 及更高版本)上启用开发人员选项的任何解决方法?

c++ - 将 Xcode 项目与 MathGL 链接时的语义问题

css - Facebook Like 社交插件大小问题

Eclipse 中的 Android Facebook SDK 4

facebook - 如何通过他们的 API 发送 Facebook 通知

ios - 如何实现嵌套 Collection View ?

objective-c - 为什么这会导致 SIGABRT 错误?

ios - 将 xcode 更新到版本 7 并将代码迁移到 swift 2。我无法运行链接错误