2017-09-14 15 views
0

我剛剛在我的應用中實現了電子郵件登錄(firebase),用戶可以在文本字段中手動添加名稱,並且當到達配置文件頁面時,找到剛剛輸入的名稱(標籤)的viewController(這是profileViewController的代碼的一部分)用戶配置文件中的Facebook名稱

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.user = Auth.auth().currentUser 

    self.databaseRef.child("user_profiles").child(self.user!.uid).observeSingleEvent(of: .value) { (snapshot:DataSnapshot) in 

     let snapshotValue = snapshot.value as? NSDictionary 

     self.name.text = snapshotValue?["name"] as? String 
     self.handle.text = snapshotValue?["handle"] as? String 

     if(snapshotValue?["about"] != nil){ 
      self.usernameField.text = snapshotValue?["about"] as? String 
     } 

     if(snapshotValue?["profile_pic"] != nil){ 
      let databaseProfilePic = snapshotValue!["profile_pic"] as! String 

      let data = try? Data(contentsOf: URL(string: databaseProfilePic)!) 

      self.setProfilePicture(imageView: self.ProfilePicture,imageToSet:UIImage(data:data!)!) 
     } 
     self.imageLoader.stopAnimating() 
    } 

} 

與Facebook登錄不同的是,用戶不輸入任何數據,但我想借此

let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"]) 

從FBSDK,所以我怎麼可以使用這些參數來讓用戶在Facebook和Facebook上登錄進入配置文件頁面找到他的名字(即fb配置文件名稱)?我試圖找到解決方案的網絡,但我還沒有找到任何東西。

+0

Firebase使用Facebook帳戶登錄,所以請使用它。 這裏是鏈接:https://firebase.google.com/docs/auth/ios/facebook-login –

+0

而不是檢查一個可選是否爲零,然後強制解包+強制轉換它,使用可選綁定('如果讓'或'警衛讓)和安全鑄造。此外,使用'try?',然後強制解包結果是沒有意義的,如果發生錯誤,您仍然會得到一個運行時異常,但不會看到它的實際原因,只會看到解壓後的力量爲零錯誤... –

+0

@JitendraModi我用它,我剛剛實施了Facebook登錄與firebase,我唯一需要的是如何在profileViewController我的標籤名稱採取Facebook用戶名 – bero

回答

0

一旦創建用戶帳戶,並通過以下方法登錄:

let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString) 

Auth.auth()?.signIn(with: credential) 


您應該能夠訪問使用用戶的基本個人資料:

Auth.auth()?.currentUser 

例如,要獲取用戶名,您應該這樣做:

guard let currentUser = Auth.auth()?.currentUser else { 
    return 
} 

userNameLabel.text = currentUser.displayName 

你也可以寫在你的數據庫中的所有用戶數據,當你第一次通過簽到方式創建用戶帳戶,像這樣:

let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString) 

// Create and sign in the user account using the Facebook access token. 

Auth.auth()?.signIn(with: credential) { (currentUser, error) in   

    guard error == nil else { 
     return 
    } 

    // Create an array of user data using authenticationData. 

    let currentUserData: [String: String] = ["name" : authenticationData.providerData["displayName"], 
    "email": authenticationData.providerData["email"] ] 

    // Write user data in the database. 

    usersRef.updateChildValues(currentUserData, withCompletionBlock: { (error, ref) in 

    guard error == nil else { 
     return 
    } 

    // ...Segue to your home controller here 

} 


然後使用observeSingleEvent(of: .value)usersRef到閱讀你的用戶資料數據。

+0

它的工作感謝亞歷克斯 – bero

+0

酷我很高興聽到它的作品@bero!祝你今天愉快 :) – Alex

相關問題