如何从FBSDKGraphRequest结果将用户名设置为UITextField

时间:2019-05-22 13:48:57

标签: swift facebook-graph-api

我正在使用Xcode 10,Swift 5,并且试图将UITextField设置为我从Facebook检索到的用户名。我已经成功检索了结果中的ID,电子邮件和姓名,但是当我将结果发送到文本字段时,它包括了所有三个字段。我只想要这个名字。

func getUserProfile() {
    let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name"], tokenString: accessToken?.tokenString, version: nil, httpMethod: "GET")
    req?.start(completionHandler: { (connection, result, error : Error!) -> Void in
        if(error == nil)
        {
            print("\(String(describing: result))")
            self.FBUserName.text = "name \(String(describing: result))"
        }
        else
        {
            print("error \(String(describing: error))")
        }
    })
}

1 个答案:

答案 0 :(得分:1)

您可以像这样将结果投射到[String : Any]

if error != nil {
    print("Error: \(error!.localizedDescription)")
} else if let result = result as? [String : Any] {
    self.FBUserName.text = result["name"] as! String
}

这是我的工作要求

let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email,name"])
graphRequest.start(completionHandler: { [weak self] (connection, result, error) -> Void in
    if error != nil {
        print("Error: \(error!.localizedDescription)")
    } else if let result = result as? [String : Any], let strongSelf = self {
        strongSelf.txtName.text = (result["name"] as! String)
        strongSelf.txtEmail.text = (result["email"] as! String)
    }
})