如何在Swift 3中处理FBSDKGRAPH请求的响应数据
我已经集成了FB最新的SDK(非swift),登录工作正常。我只需要知道如何解析图形响应数据,因为它不是有效的JSON 工作代码:如何在Swift 3中处理FBSDKGRAPH请求的响应数据,swift,xcode,facebook-graph-api,swift3,Swift,Xcode,Facebook Graph Api,Swift3,我已经集成了FB最新的SDK(非swift),登录工作正常。我只需要知道如何解析图形响应数据,因为它不是有效的JSON 工作代码: func configureFacebook() { login.readPermissions = ["public_profile", "email", "user_friends"]; login.delegate = self } func loginButton(
func configureFacebook()
{
login.readPermissions = ["public_profile", "email", "user_friends"];
login.delegate = self
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
print("Login buttoon clicked")
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print(result)
}
})
}
输出:
Login button clicked
Optional({
"first_name" = KD;
id = 10154CXXX;
picture = {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/XXXn.jpg?oh=a75a5c1b868fa63XXX146A";
};
};
})
那么,我应该以什么格式转换上述数据以获得URL或first_name等值呢
我还尝试转换到NSDictionary
,但出现错误:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
print("Login buttoon clicked")
let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"first_name,email, picture.type(large)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
print("Error: \(error)")
}
else
{
do {
let fbResult = try JSONSerialization.jsonObject(with: result as! Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
print(fbResult.value(forKey: "name"))
} catch {
print(error)
}
}
})
}
您只需按如下方式进行操作:
let data:[String:AnyObject] = result as! [String : AnyObject]
print(data["first_name"]!)
Swift 3:
安全展开&Any
而不是AnyObject
if let data = result as? [String:Any] {
}
Swift 5问题、问题和解决方案代码: 实施: 输出:
旁白:多亏了KD和Bista,他们的提问和回答帮助我解决了这个问题。这似乎是最简单的。我的最终代码。让数据:[字符串:AnyObject]=结果为![String:AnyObject]让urlData=data[“picture”]?.value(forKey:“data”)为![String:AnyObject]让url=urlData[“url”]!尝试像以前一样访问配置文件图片,但现在返回了nil,即使有信息:
let profilePictureURLStr=data[“picture.data.url”]as?字符串
。出现问题。您使用的是swiftyJSON吗?请检查我的代码问题:
let graphRequest:GraphRequest = GraphRequest(graphPath: "me", parameters: ["fields":"first_name,email,picture.type(large)"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil) {
print("Error: \(String(describing: error))")
}
else {
guard let rDic = result as? NSDictionary else {
SVProgressHUD.showError(withStatus: "facebook Did not allowed loading email, please set that while updating profile.")
return
}
print("rDic = ", rDic)
}
})
rDic = {
email = "you@know.it";
"first_name" = Mr. Me;
id = #################;
picture = {
data = {
height = 32;
"is_silhouette" = 0;
url = "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=################&height=200&width=200&ext=################&hash=################";
width = 32;
};
};
}