我在关注 this guide 后尝试自定义 Facebook 登录按钮.一切都正确集成,我可以登录 Facebook,并顺利注销。
现在我的下一个目标是让屏幕看起来像我的模型,这包括将登录按钮正确地放置在 Storyboard上,但唯一的问题是它没有出现在故事板上,它只是在我运行模拟。我查看了其他溢出答案,但它们并没有真正帮助,因为它们适用于早期版本的 swift/Xcode,并且无法按照评论所说的进行工作。我的代码与指南完全相同,因为这是我尝试实现的第一个屏幕。
非常感谢您的帮助。
最佳答案
如果您想使用自定义 facebook SDK 按钮,您可以在 Storyboard中创建任何自定义按钮,并像这样在 View Controller 中提供操作
在顶部导入
import FBSDKCoreKit
import FBSDKLoginKit
swift 3:
@IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result!
// if user cancel the login
if (result?.isCancelled)!{
return
}
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
旧的 Swift 版本:
@IBAction func loginFacebookAction(sender: AnyObject) {//action of the custom button in the storyboard
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
}
}
}
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
//everything works print the user data
print(result)
}
})
}
}
如果您想要默认 facebook 登录 按钮,您可以使用 UIView 并在身份检查器中设置自定义类**部分的类*字段,我们必须设置 FBLoginValue
引用:默认 SDK 登录按钮教程:http://www.appcoda.com/ios-programming-facebook-login-sdk/
引用:用于参数和更多信息的自定义 SDK 登录按钮:https://developers.facebook.com/docs/facebook-login/ios
关于ios - 自定义 Facebook 登录按钮 - 集成后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380389/