【发布时间】:2017-02-11 05:46:57
【问题描述】:
在 Swift 2.2 和 iOS 9 之前它可以完美运行,但在 Swift 3.0 中进行更改后,我无法获取姓名、电子邮件和 fbid。这是因为结果来自任何并且无法从结果中提取数据。在 swift 3.0 中转换后,我的代码如下所示
func signInWithFB(_ forVC:UIViewController , sucess:@escaping signInSuccessBlock , falure:@escaping SignInFailureBlock)
{
let obj_facebook = FBSDKLoginManager()
obj_facebook.loginBehavior = .native
obj_facebook.logIn(withReadPermissions: ["public_profile","email","user_friends"], from: forVC) { (result:FBSDKLoginManagerLoginResult?, error:Error?) -> Void in
if ((error) != nil)
{
falure(error as NSError?, false)
}
else if ((result?.isCancelled) == true)
{
falure(nil, true)
}
else
{
let dicLoginData = NSMutableDictionary()
let request = FBSDKGraphRequest(graphPath: "/me?fields=name,email,gender,hometown,education,location,first_name,last_name", parameters: nil)
let _ =
request?.start(completionHandler: { (Connection:FBSDKGraphRequestConnection?, result:Any!, error:Error?) -> Void in
if error == nil
{
dicLoginData.setValue((result as AnyObject).value(forKey: "name"), forKey: "name")
dicLoginData.setValue((result as AnyObject).value(forKey: "email"), forKey: "email")
dicLoginData.setValue((result as AnyObject).value(forKey: "gender"), forKey: "Gender")
dicLoginData.setValue((result as AnyObject).value(forKey: "last_name"), forKey: "lname")
dicLoginData.setValue((result as AnyObject).value(forKey: "first_name"), forKey: "fname")
let faceBookID:String = (result as AnyObject).value(forKey: "id") as! String
let proPicUrl = String(format:"https://graph.facebook.com/%@/picture?type=large", faceBookID)
dicLoginData.setValue(faceBookID, forKey: "facebookId")
dicLoginData.setValue(proPicUrl, forKey: "proPicURL")
// dicLoginData.setValue(strLoaction?.first, forKey: "City")
sucess(dicLoginData)
}
else
{
falure(error as NSError?, false)
}
})
}
}
}
【问题讨论】:
标签: ios swift facebook-graph-api dictionary swift3