我正在尝试学习有关Swift和Facebook的知识。 我没有使用Pod手动集成了SDK,到目前为止一切正常! 我通过遵循一些在线指南创建了一个“登录”按钮,该按钮也适用! 通过位于ViewController中的这段代码,我还可以打印ID,First_Name,Last_Name并接收ProfilePicture信息:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if(error == nil)
{
print(result as Any)
}
})
} else
{
print(error.localizedDescription)
}
}
func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -> Bool {
return true
}
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Disconnected")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}
}
输出是这个:
Optional({
email = "cxxxxxxxxx2@live.it";
"first_name" = Cxxxxxe;
id = 10xxxxxxxxxxxxx75;
"last_name" = Lxxxxxo;
picture = {
data = {
height = 200;
"is_silhouette" = 0;
url = "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=10xxxxxxxxxxxxx75&height=200&width=200&ext=1565467901&hash=AeQ-NalWNEMh91CK";
width = 200;
};
};
})
现在,我不知道如何在变量中提取单个信息(例如first_name)以将其打印在标签中,或者获取图像的网址以在应用程序中将其打印。
如果我尝试在登录功能内部进行如下更改:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if(error == nil)
{
print(result!["first_name"] as Any)
}
})
} else
{
print(error.localizedDescription)
}
}
编译器告诉我:“'Any'类型的值没有下标”。
我该怎么做?
谢谢;)
答案 0 :(得分:0)
在结果中应该放
result.user
该对象就是包含诸如displayName或firstName之类的属性的对象
答案 1 :(得分:0)
我创建了标签,现在我以这种方式解决了这个问题:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
print("id: \(id)")
self.lbl_fbid.text = "ID: \(id)"
}
if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
print("first_name: \(first_name)")
self.lbl_fbdirstname.text = "First_Name: \(first_name)"
}
if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
print("last_name: \(last_name)")
self.lbl_fblastname.text = "First_Name: \(last_name)"
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
self.lbl_fbemail.text = "Email: \(email)"
}
})
} else
{
print(error.localizedDescription)
}
}
现在我只需要弄清楚如何拍摄个人资料照片...
答案 2 :(得分:0)
全部完成...如果有人需要我写的代码:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if (error == nil) {
print("Connected")
let r = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,first_name,last_name,picture.type(large)"], tokenString: FBSDKAccessToken.current()?.tokenString, version: nil, httpMethod: "GET")
r?.start(completionHandler: { (test, result, error) in
if let id : NSString = (result! as AnyObject).value(forKey: "id") as? NSString {
print("id: \(id)")
self.lbl_fbid.text = "ID: \(id)"
}
if let imageURL = (((((result! as AnyObject).value(forKey: "picture")) as AnyObject).value(forKey: "data")) as AnyObject).value(forKey: "url") as? NSString{
print("img url: \(imageURL)")
let url = URL(string: imageURL as String)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
self.img_fbprofilepic.image = UIImage(data: data!)
}
}
}
if let first_name : NSString = (result! as AnyObject).value(forKey: "first_name") as? NSString {
print("first_name: \(first_name)")
self.lbl_fbdirstname.text = "First_Name: \(first_name)"
}
if let last_name : NSString = (result! as AnyObject).value(forKey: "last_name") as? NSString {
print("last_name: \(last_name)")
self.lbl_fblastname.text = "First_Name: \(last_name)"
}
if let email : NSString = (result! as AnyObject).value(forKey: "email") as? NSString {
print("email: \(email)")
self.lbl_fbemail.text = "Email: \(email)"
}
if(error == nil)
{
print(result as Any)
}
})
} else {
print(error.localizedDescription)
}
}