获取Facebook API配置文件图片ios Swift

时间:2016-05-24 12:33:37

标签: ios json swift facebook

我有兴趣从用户的fb个人资料中获取以下信息:

  1. 电子邮件
  2. 名称
  3. 个人资料图片链接
  4. 年龄
  5. 为此,我使用以下代码:

    let accessToken = FBSDKAccessToken.currentAccessToken()
        let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"picture.redirect(false),email,name,gender,age_range,birthday"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
        req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
            if(error == nil)
            {
                print("result \(result)")
            }
            else
            {
                print("error \(error)")
            }
        })    }
    

    返回:

    result {
    "age_range" =     {
        min = 21;
    };
    email = "dedewdd@gmail.com";
    gender = male;
    id = xxxxxxxxxxxx;
    name = "John Smith";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/dwqdwddqwdwqdw";
        };
    };
    }
    

    这个问题是这个图像是50X50像素,我想获得一个更大图像的链接。

4 个答案:

答案 0 :(得分:2)

所以我使用Kingfisher下载图像,但基本上NSURL是获得更大图像的关键。如果这不起作用,那么尝试更改" 50x50"在" 250x250"的网址中看看是否有效

if FBSDKAccessToken.currentAccessToken() != nil {

let userID = FBSDKAccessToken.currentAccessToken().userID

if(userID != nil) //should be != nil
{
print(userID)
}

let URL = NSURL(string: "http://graph.facebook.com/\(userID)/picture?type=large")!

profileImage.kf_setImageWithURL(URL)

}else{

}
}

答案 1 :(得分:1)

试试这个我希望它会有所帮助!!

@IBAction func FacebookLogin(sender: AnyObject) {

    var message = String()
    if Reachability.isConnectedToNetwork() == true {

        let loginView : FBSDKLoginManager = FBSDKLoginManager()
        loginView.loginBehavior = FBSDKLoginBehavior.Web

        if (FBSDKAccessToken .currentAccessToken() != nil) {
            loginView.logOut()
        }else{

            loginView.logInWithReadPermissions(["email","public_profile"], fromViewController: self, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) in

                if (error != nil){
                    print("Login Process Eroor!"+error.localizedDescription)
                }else if result.isCancelled{
                    print("User cancled Login")
                }else{
                    print("Login Success")
                    if result.grantedPermissions .contains("email"){
                        self.fetchUserInfo()
                    }else{
                         message = message.stringByAppendingString("Facebook email permission error")
                        WebService.ShowToastMessage(message, viewcontroller: self)
                    }
                }
            })
        }

    } else {
        message = message.stringByAppendingString("Make sure your device is connected to the internet.")
        WebService.ShowToastMessage(message, viewcontroller: self)
    }
}
func fetchUserInfo() -> Void {

    if (FBSDKAccessToken .currentAccessToken() != nil) {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,gender,birthday,email,name,picture.width(480).height(480)"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                print("Error: \(error.localizedDescription)")
            }
            else
            {
                print("fetched user: \(result)")
                let id : NSString = result.valueForKey("id") as! String
                let name:NSString = result.valueForKey("name") as! String
                let gender:NSString = result.valueForKey("gender") as! String
                let email:NSString = result.valueForKey("email") as! String
                print("User ID is: \(id)")
                print("User email is:\(email)")
                print("User name is :\(name)")
                print("User gender is :\(gender)")

            }
        })
    }
}

答案 2 :(得分:0)

根据facebook docs

https://developers.facebook.com/docs/graph-api/reference/user/picture/

您可以传递图像的宽度和高度,以下是参数列表.. enter image description here

所以你可以试试像..

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

}]

答案 3 :(得分:0)

使用此代码通过一个请求获取用户ID,名称和个人资料图片:https://stackoverflow.com/a/45462114/1168602

在xcode 8.3.3上测试