如何在 Swift 3 中使用 Facebook API 访问个人资料图片?

How to access profile picture with Facebook API in Swift 3?

早些时候我尝试这样访问:

let gr2 : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,picture.width(198).height(198)"])
gr2.start(completionHandler: { (connection, result2, error) -> Void in                
let data = result2 as! [String : AnyObject]
let _loggedInUserSettingRecordName = data["id"] as? String // (forKey: "id") as? String
let profilePictureURLStr = data["picture.data.url"] as? String

但是profilePictureURLStr现在是零了。 Swift 3 有什么不同?

我可以看到 url 信息是其中的一部分:

(lldb) po result2
▿ Optional<Any>
  ▿ some : 2 elements
    ▿ 0 : 2 elements
      - .0 : id
      - .1 : 10208273026137463
    ▿ 1 : 2 elements
      - .0 : picture
      ▿ .1 : 1 element
        ▿ 0 : 2 elements
          - .0 : data
          ▿ .1 : 4 elements
            ▿ 0 : 2 elements
              - .0 : is_silhouette
              - .1 : 0
            ▿ 1 : 2 elements
              - .0 : height
              - .1 : 200
            ▿ 2 : 2 elements
              - .0 : url
              - .1 : https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13418946_10208553701714177_3969269576626117653_n.jpg?oh=add39246ec9693ecead0529ecbbbfc53&oe=5862D7ED
            ▿ 3 : 2 elements
              - .0 : width
              - .1 : 200

我尝试了很多可能性,这样你可以得到 url:

let profilePictureURLStr = (data["picture"]!["data"]!! as! [String : AnyObject])["url"]

这种方式也适用于 Swift 3:

if let picture = data["picture"] as? Dictionary<String,Any> {
                        if let data = picture["data"] as? Dictionary<String,Any> {
                            if let pictureUrl = data["url"] as? String {
                                dict["remote_main_image_url"] = pictureUrl
                            }
                        }
                    }