Get email and name Facebook SDK v4.4.0 Swift

前端 未结 10 2116
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 02:22

TL;TR: How do I get the email and name of a user that is logged in on my app using the facebook SDK 4.4

So far I have managed to get login working, now I can get the

相关标签:
10条回答
  • 2020-12-08 02:53

    facebook ios sdk get user name and email swift 3

    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil) {
                let fbDetails = result as! NSDictionary
                print(fbDetails)
            } else {
                print(error?.localizedDescription ?? "Not found")
            }
        })
    
    0 讨论(0)
  • 2020-12-08 02:53

    In Swift 4.2 and Xcode 10.1

    @IBAction func onClickFBSign(_ sender: UIButton) {
    
        if let accessToken = AccessToken.current {
            // User is logged in, use 'accessToken' here.
            print(accessToken.userId!)
            print(accessToken.appId)
            print(accessToken.grantedPermissions!)
            print(accessToken.expirationDate)
    
            let request = GraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,first_name,last_name,picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
            request.start { (response, result) in
                switch result {
                case .success(let value):
                    print(value.dictionaryValue!)
                case .failed(let error):
                    print(error)
                }
            }
    
            let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
            self.present(storyboard, animated: true, completion: nil)
        } else {
    
            let loginManager=LoginManager()
    
            loginManager.logIn(readPermissions: [ReadPermission.publicProfile, .email, .userFriends, .userBirthday], viewController : self) { loginResult in
                switch loginResult {
                case .failed(let error):
                    print(error)
                case .cancelled:
                    print("User cancelled login")
                case .success(let grantedPermissions, let declinedPermissions, let accessToken):
                    print("Logged in : \(grantedPermissions), \n \(declinedPermissions), \n \(accessToken.appId), \n \(accessToken.authenticationToken), \n \(accessToken.expirationDate), \n \(accessToken.userId!), \n \(accessToken.refreshDate), \n \(accessToken.grantedPermissions!)")
    
                    let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
                    request.start { (response, result) in
                        switch result {
                        case .success(let value):
                            print(value.dictionaryValue!)
                        case .failed(let error):
                            print(error)
                        }
                    }
    
                    let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
                    self.navigationController?.pushViewController(storyboard, animated: true)
    
                }
            }
        }
    
    }
    

    For complete details https://developers.facebook.com/docs/graph-api/reference/user

    0 讨论(0)
  • 2020-12-08 02:54

    you can use this code to get email ,name and profile picture of user

       @IBAction func fbsignup(_ sender: Any) {
        let fbloginManger: FBSDKLoginManager = FBSDKLoginManager()
        fbloginManger.logIn(withReadPermissions: ["email"], from:self) {(result, error) -> Void in
            if(error == nil){
                let fbLoginResult: FBSDKLoginManagerLoginResult  = result!
    
                if( result?.isCancelled)!{
                    return }
    
    
                if(fbLoginResult .grantedPermissions.contains("email")){
                    self.getFbId()
                }
            }  }
    
    }
    func getFbId(){
    if(FBSDKAccessToken.current() != nil){
    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email,picture.type(large)"]).start(completionHandler: { (connection, result, error) in
        guard let Info = result as? [String: Any] else { return } 
    
                if let imageURL = ((Info["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
            //Download image from imageURL
        }
    if(error == nil){
    print("result")
    }
    })
    }
    }
    
    0 讨论(0)
  • 2020-12-08 02:56

    Call the below function after you logged in via Facebook.

       func getUserDetails(){
    
        if(FBSDKAccessToken.current() != nil){
    
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name , first_name, last_name , email"]).start(completionHandler: { (connection, result, error) in
    
                guard let Info = result as? [String: Any] else { return }
    
                 if let userName = Info["name"] as? String
                    {
                       print(userName)
                    }
    
            })
        }
    }
    
    0 讨论(0)
提交回复
热议问题