2015-07-02 28 views
5

Mam funkcję pomocnika, który jest pobieranie obrazu z określonego adresu URL:Odzyskaj Facebook User ID w Swift

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 
} 

przystąpię zadzwonić getProfilePicture() z użytkownika Facebooka ID i przechowywać dane wyjściowe do UIImageView. Moje pytanie brzmi: jak mogę znaleźć identyfikator użytkownika Facebooka? Czy żądam połączenia za pośrednictwem Facebooka? Byłoby pomocne, gdyby jakiś kod został tutaj podany.

Dziękuję za pomoc.

Odpowiedz

1

Co pracował dla mnie:

  1. Zastosowanie FBSDKLoginButtonDelegate
  2. Wdrożenie funkcjono loginButtonWillLogin
  3. Dodaj powiadomienie onProfileUpdated
  4. W onProfileUpdated func postępować, aby uzyskać Informacje o profilu
  5. Po onProfileUpdated nazywa, możesz użyć swojego FBSDKProfil w dowolnym kontrolerze

A teraz kilka Swift code 3 ...

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!)") 
} 
} 
6

Jeśli są już pomyślnie zalogowany do aplikacji:

FBSDKAccessToken.current().userID 
Powiązane problemy