I have a helper function that is grabbing the picture from a specific URL:
func getProfilePicture(fid: String) -> UIImage? {
if (fid != "") {
var imageURLString = "http://graph.facebook.com/" + fid + "/picture?type=large"
var imageURL = NSURL(string: imageURLString)
var imageData = NSData(contentsOfURL: imageURL!)
var image = UIImage(data: imageData!)
return image
}
return nil
}
I proceed to call getProfilePicture() with the user's Facebook ID, and store the output into a UIImageView. My question is, how can I find a Facebook user's ID? Do I request a connection through Facebook? It would be helpful if some code is provided here.
Thank you for your help.
If they are already successfully logged into your app:
FBSDKAccessToken.current().userID
What worked for me:
And now, some Swift 3 code...
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Facebook login integration
let logBtn = FBSDKLoginButton()
logBtn.center = self.view.center
self.view .addSubview(logBtn)
logBtn.delegate = self
}
func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -> Bool {
print("will Log In")
FBSDKProfile.enableUpdates(onAccessTokenChange: true)
NotificationCenter.default.addObserver(self, selector: #selector(onProfileUpdated(notification:)), name:NSNotification.Name.FBSDKProfileDidChange, object: nil)
return true
}
func onProfileUpdated(notification: NSNotification)
{
print("profile updated")
print("\(FBSDKProfile.current().userID!)")
print("\(FBSDKProfile.current().firstName!)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With