IOS - Facebook登录按钮:无法识别的选择器错误

时间:2015-04-30 19:19:24

标签: ios facebook swift

我是iOS和Swift的新手,请原谅我对这个主题的无知。

我正在尝试将Facebook登录按钮添加到我的Main.storyboard。我添加了一个视图对象,并将视图的类设置为FBSDKLoginButton。但是,当我在模拟器中运行项目时,它会因无法识别的选择器错误而崩溃。

根据这个https://developers.facebook.com/docs/ios/troubleshooting#unrecognizedselector,我需要在我的app委托中添加[FBSDKLoginButton class];或添加-ObjC链接器标记。

问题是,Swift中该代码的等价物是什么?或者我应该添加-ObjC链接器标志?哪一个更受欢迎?

2 个答案:

答案 0 :(得分:4)

我有同样的错误。

经过检查,我的项目中出现了我需要的东西:FBSDKCoreKit,FBSDKLoginKit和FBSDKShareKit。

如果你使用cocoapods,这就是Podfile中的一行:

pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
pod 'FBSDKShareKit'

pod install之后,将其添加到您的桥接标题.h文件中:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

在故事板中,定义类型为FBSDKLoginButton的视图:

enter image description here

答案 1 :(得分:3)

以下是我使用的实现

         override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view, typically from a nib.

                if (FBSDKAccessToken.currentAccessToken() != nil)
                {
                    // User is already logged in, do work such as go to next view controller.
                }
                else
                {
                    let loginView : FBSDKLoginButton = FBSDKLoginButton()
                    self.view.addSubview(loginView)
                    loginView.center = self.view.center
                    loginView.readPermissions = ["public_profile", "email", "user_friends"]
                    loginView.delegate = self
                }

            }

    // Facebook Delegate Methods

        func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
            println("User Logged In")

            if ((error) != nil)
            {
                // Process error
            }
            else if result.isCancelled {
                // Handle cancellations
            }
            else {
                // If you ask for multiple permissions at once, you
                // should check if specific permissions missing
                if result.grantedPermissions.contains("email")
                {
                     // Do work
                }
            }   
        }



        func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
            println("User Logged Out")
}



     func returnUserData()
        {
            let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
            graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

                if ((error) != nil)
                {
                    // Process error
                    println("Error: \(error)")
                }
                else
                {
                    println("fetched user: \(result)")
                    let userName : NSString = result.valueForKey("name") as! NSString
                    println("User Name is: \(userName)")
                    let userEmail : NSString = result.valueForKey("email") as! NSString
                    println("User Email is: \(userEmail)")
                }
            })
        }

提到Link