使用 PFFacebookUtils 和 Swift 和 iOS 9 获取 Facebook 帐户详细信息
Posted
技术标签:
【中文标题】使用 PFFacebookUtils 和 Swift 和 iOS 9 获取 Facebook 帐户详细信息【英文标题】:Get Facebook account details using PFFacebookUtils with Swift and iOS 9 【发布时间】:2016-01-11 16:33:01 【问题描述】:我正在使用 Facebook 实现社交登录。 我使用以下代码记录用户并“检索”与他的帐户相关的信息(我希望),
PFFacebookUtils.logInInBackgroundWithReadPermissions(["public_profile", "email", "user_friends"])
(user: PFUser?, error: NSError?) -> Void in
if let user = user
if user.isNew
print("User signed up and logged in through Facebook!")
else
print("User logged in through Facebook!")
print(user)
else
print("Uh oh. The user cancelled the Facebook login.")
但控制台输出(和要解析的浏览数据)仅显示一个 ID,没有与此 Facebook 帐户相关联的信息。
我读到要获取用户详细信息,我必须在登录请求后发送 FBSDKGraphRequest。 如果是这样,beetwin 在 Parse 实现中使用登录而不是链接有什么区别(以及优势是什么)?
有没有办法在登录操作后自动导入与“权限”Facebook 帐户相关的信息?
提前谢谢你, 米歇尔
【问题讨论】:
【参考方案1】:只需根据您的需要编辑读取权限,我没有使用 PFF,但 FBSDK 为我完成了工作,希望它对您有用
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ProfileViewController: UIViewController, FBSDKLoginButtonDelegate
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var fbLoginButton: FBSDKLoginButton!
override func viewDidLoad()
super.viewDidLoad()
self.fbLoginButton.delegate = self
self.fbLoginButton.readPermissions = ["public_profile"]
self.fbLoginButton.publishPermissions = ["publish_actions"]
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "fbProfileChanged:",
name: FBSDKProfileDidChangeNotification,
object: nil)
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
// If we have a current Facebook access token, force the profile change handler
if ((FBSDKAccessToken.currentAccessToken()) != nil)
self.fbProfileChanged(self)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func prefersStatusBarHidden() -> Bool
return true
//facebooks functions
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
if (error != nil)
print( "\(error.localizedDescription)" )
else if (result.isCancelled)
// Logged out?
print( "Login Cancelled")
else
// Logged in?
print( "Logged in, segue now")
self.performSegueWithIdentifier("showHome", sender: self)
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
//see bitfountain
func fbProfileChanged(sender: AnyObject!)
let fbProfile = FBSDKProfile.currentProfile()
if (fbProfile != nil)
// Fetch & format the profile picture
let strProfilePicURL = fbProfile.imagePathForPictureMode(FBSDKProfilePictureMode.Square, size: imageView.frame.size)
let url = NSURL(string: strProfilePicURL, relativeToURL: NSURL(string: "http://graph.facebook.com/"))
let imageData = NSData(contentsOfURL: url!)
let image = UIImage(data: imageData!)
self.nameLabel.text = fbProfile.name
self.imageView.image = image
self.nameLabel.hidden = false
self.imageView.hidden = false
self.nextButton.hidden = false
else
self.nameLabel.text = ""
self.imageView.image = UIImage(named: "")
self.nameLabel.hidden = true
self.imageView.hidden = true
@IBAction func nextButtonPressed(sender: UIButton)
self.performSegueWithIdentifier("showHome", sender: self)
【讨论】:
以上是关于使用 PFFacebookUtils 和 Swift 和 iOS 9 获取 Facebook 帐户详细信息的主要内容,如果未能解决你的问题,请参考以下文章
localDataStore 和 PFFacebookUtils 崩溃
用户在使用 PFFacebookUtils 登录时取消选择 Facebook 权限
使用 Cloud Code 和 PFFacebookUtils 覆盖电子邮件/用户名
PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions) 挂起应用