gpt4 book ai didi

ios - 如何在 Swift 3 中获取 Facebook 用户 ID、电子邮件、图片类型(大)

转载 作者:可可西里 更新时间:2023-11-01 00:36:04 26 4
gpt4 key购买 nike

我尝试从 Facebook SDK 获取用户 id, email, picture.type(large),updated_time,我成功获取了所有这些但我没有获取图片给我:

Error Cannot call value of non-function type 'Any?!'

下面是我的清晰代码。

    import UIKit
import Foundation

class LoginViewController : UIViewController, FBSDKLoginButtonDelegate {
private var fromLogin = [Login]()
@IBOutlet weak var loginButton : FBSDKLoginButton!

override func viewDidLoad() {
super.viewDidLoad()
// Check Session
if let token = FBSDKAccessToken.current(){
print(token.tokenString! as Any)
self.fetchProfile()
}

loginButton.delegate = self
loginButton.readPermissions = ["email", "public_profile"]
}

// Get Profile
func fetchProfile() {
print("fetch profile")

// Create facebook graph with fields
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id, name, email, picture.type(large), updated_time"]).start { (connection, result, error) in

// check error
if error != nil {
// error happen
print("Failed to start graph request: \(error?.localizedDescription)")
return
}

if let userDict = result as? NSDictionary {
let name = userDict["name"] as? String
let id = userDict["id"] as? String
let email = userDict["email"] as? String

// HERE GIVES ERROR I DIDNT GET PICTURE WITH STRING
let userPicture = userDict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String

print(userPicture)
print(name as Any)
print(id as Any)
print(email as Any)
}
}
}

// Delegate Method
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!){
// check error
if error != nil {
// error happen
print(error?.localizedDescription as Any)
return
}

print("Successfull login in with facebook...")
fetchProfile()
}

func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!){
print("Log Out")
}

这一行报错;

let userPicture = userDict.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as String

此处还有图片对象json数据示例;

 picture =     {
data = {
"is_silhouette" = 0;
url = "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14067524_9513823423422249533237851_n.jpg?oh=88eb9b80abbb2342346c01de298&oe=58C5138B";
};
};

我正在使用 Xcode 8.1Swift 3.0

最佳答案

这需要简单的解析..“图片”是字典,“数据”也是。所以像下面这样的东西适合你

guard let userDict = result as? [String:Any] else { return }

let name = userDict["name"] as? String
let id = userDict["id"] as? String
let email = userDict["email"] as? String

if let picture = userDict["picture"] as? [String:Any] ,
let imgData = picture["data"] as? [String:Any] ,
let imgUrl = imgData["url"] as? String {
print(imgUrl)
}

关于ios - 如何在 Swift 3 中获取 Facebook 用户 ID、电子邮件、图片类型(大),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40608993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com