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

前端 未结 10 1250
天涯浪人
天涯浪人 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:41

    Swift 4 approach :-

    private func fetchUserData() {
        let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, email, name, picture.width(480).height(480)"])
        graphRequest?.start(completionHandler: { (connection, result, error) in
            if error != nil {
                print("Error",error!.localizedDescription)
            }
            else{
                print(result!)
                let field = result! as? [String:Any]
                self.userNameLabel.text = field!["name"] as? String
                if let imageURL = ((field!["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
                    print(imageURL)
                    let url = URL(string: imageURL)
                    let data = NSData(contentsOf: url!)
                    let image = UIImage(data: data! as Data)
                    self.profileImageView.image = image
                }
            }
        })
    }
    

提交回复
热议问题