Facebook iOS SDK and swift: how get user's profile picture

前端 未结 10 1550
天涯浪人
天涯浪人 2021-01-31 03:51

i have integrated Facebook sdk in Xcode 6 (with swift). During the login i request the public_profile permission:

FBSession.openActiveSessionWithReadPermissions(         


        
10条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 04:34

    For Swift 5

    First add the fields that you need

    let params = ["fields": "first_name, last_name, email, picture"]
    

    Create the graph request

    let graphRequest = GraphRequest(graphPath: "me", parameters: params, tokenString: token.tokenString, version: nil, httpMethod: .get)
    graphRequest.start { (connection, result, error) in }
    

    You will get the result in json

    {
      "first_name": "",
      "last_name": "",
      "picture": {
        "data": {
          "height": 50,
          "is_silhouette": false,
          "url": "",
          "width": 50
        }
      },
      "id": ""
    }
    

    According to the json response, catch the result

    if let error = error {
                print("Facebook graph request error: \(error)")
            } else {
                print("Facebook graph request successful!")
                guard let json = result as? NSDictionary else { return }
                if let id = json["id"] as? String {
                    print("\(id)")
                }
                if let email = json["email"] as? String {
                    print("\(email)")
                }
                if let firstName = json["first_name"] as? String {
                    print("\(firstName)")
                }
                if let lastName = json["last_name"] as? String {
                    print("\(lastName)")
                }
                if let profilePicObj = json["picture"] as? [String:Any] {
                    if let profilePicData = profilePicObj["data"] as? [String:Any] {
                        print("\(profilePicData)")
                        if let profilePic = profilePicData["url"] as? String {
                            print("\(profilePic)")
                        }
                    }
                }
            }
        }
    

    You can also get custom width profile image by sending the required width in the params

    let params = ["fields": "first_name, last_name, email, picture.width(480)"]
    

    This is how the whole code would like

    if let token = AccessToken.current {
                let params = ["fields": "first_name, last_name, email, picture.width(480)"]
                let graphRequest = GraphRequest(graphPath: "me", parameters: params,
                                                tokenString: token.tokenString, version: nil, httpMethod: .get)
                graphRequest.start { (connection, result, error) in
                    if let error = error {
                        print("Facebook graph request error: \(error)")
                    } else {
                        print("Facebook graph request successful!")
                        guard let json = result as? NSDictionary else { return }
                        if let id = json["id"] as? String {
                            print("\(id)")
                        }
                        if let email = json["email"] as? String {
                            print("\(email)")
                        }
                        if let firstName = json["first_name"] as? String {
                            print("\(firstName)")
                        }
                        if let lastName = json["last_name"] as? String {
                            print("\(lastName)")
                        }
                        if let profilePicObj = json["picture"] as? [String:Any] {
                            if let profilePicData = profilePicObj["data"] as? [String:Any] {
                                print("\(profilePicData)")
                                if let profilePic = profilePicData["url"] as? String {
                                    print("\(profilePic)")
                                }
                            }
                        }
                    }
                }
            }
    

    Check out Graph API Explorer for more fields.

提交回复
热议问题