Facebook登录Swift后加载另一个ViewController

时间:2016-02-11 12:48:32

标签: ios swift uiviewcontroller facebook-login

我正在尝试使用swift成功登录Facebook后加载另一个视图控制器。我一直关注此博文:http://studyswift.blogspot.com.tr/2016/01/facebook-sdk-and-swift-create-facebook.html

我配置了一个名为“ redirectAfterLogin ”的segue,并尝试使用 performSegueWithIdentifier

下面你可以看到我的ViewController.swift和AppDelegate.swift文件。不知怎的,我错过了什么。有什么建议吗?

ViewController.swift

controllerAs

AppDelegate.swift

this

这是我在模拟器中登录后的输出:

  

2016-02-11 14:43:49.712 TMR-Prototype1 [22260:3512428] -canOpenURL:URL失败:“fbauth2:/” - 错误:“(null)”

     

2016-02-11 14:43:49.717 TMR-Prototype1 [22260:3512428] -canOpenURL:URL失败:“fbauth2:/” - 错误:“(null)”

这是截图。它停留在第一个viewcontroller: enter image description here

我还将Info.plist文件编辑为:

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let loginButton = FBSDKLoginButton()
        loginButton.center = view.center
        view.addSubview(loginButton)
        //self.performSegueWithIdentifier("redirectAfterLogin", sender: self)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result:FBSDKLoginManagerLoginResult!, error: NSError!) {
        if error == nil {
            print("login complete")
            self.performSegueWithIdentifier("redirectAfterLogin", sender: self)
        }
        else {
            print(error.localizedDescription)
        }

    }
    */

}

1 个答案:

答案 0 :(得分:4)

您应该FBSDKLoginButtonDelegate实施ViewController,并包含委托方法。

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let loginButton = FBSDKLoginButton()
        loginButton.center = view.center
        loginButton.delegate = self // Remember to set the delegate of the loginButton
        view.addSubview(loginButton)
    }

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        /* 
           Check for successful login and act accordingly.
           Perform your segue to another UIViewController here.
        */
    }

    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
        // Actions for when the user logged out goes here
    }
}