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

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

    If you want to get the picture in the same request as the rest of the users information you can do it all in one graph request. It's a little messy but it beats making another request.

    A more Swift 3 approach

    let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
    let _ = request?.start(completionHandler: { (connection, result, error) in
        guard let userInfo = result as? [String: Any] else { return } //handle the error
    
        //The url is nested 3 layers deep into the result so it's pretty messy
        if let imageURL = ((userInfo["picture"] as? [String: Any])?["data"] as? [String: Any])?["url"] as? String {
            //Download image from imageURL
        }
    })
    

    Swift 2

    let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
    request.startWithCompletionHandler({ (connection, result, error) in
        let info = result as! NSDictionary
        if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String {
            //Download image from imageURL
        }
    })
    

提交回复
热议问题