KD.*_*KD. 13 xcode facebook-graph-api swift swift3
我已经集成了FB最新的SDK(非swift),登录工作正常.我需要知道如何解析Graph响应数据,因为它不是有效的JSON
工作代码:
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)
}
})
}
Run Code Online (Sandbox Code Playgroud)
随着输出:
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";
};
};
})
Run Code Online (Sandbox Code Playgroud)
那么我应该将上述数据转换为什么格式来获取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)
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
Bis*_*sta 21
您可以按如下方式执行此操作:
let data:[String:AnyObject] = result as! [String : AnyObject]
print(data["first_name"]!)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
安全展开Any而不是AnyObject
if let data = result as? [String:Any] {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9868 次 |
| 最近记录: |