【问题标题】:set the results of facebook integration to labels将 facebook 集成的结果设置为标签
【发布时间】:2016-11-07 10:25:11
【问题描述】:

我正在使用 Swift 3.0 开发一个项目,并且我有一个使用 Facebook 的身份验证注册程序。所以基本上我已经完成了 FB 集成部分,并且一旦登录到一个帐户,我就得到了用户的电子邮件地址、姓名、ID 和个人资料图片的 url,因为我已经请求了他们(这就是我进入我的控制台)。我收到的格式如下。

Optional({
    email = "xxxxx@yahoo.com";
    id = xxxxxxxxxx;
    name = "xxxxxx xxxx";
    picture =     {
        data =         {
            "is_silhouette" = 0;
            url = "https://scontent.xx.fbcdn.net/v/t1-1/c2.0.50.50/p50x50/10441341_10201209786847369_5426097469926927988_n.jpg?oh=1b4d8709be17338bc7e877d411b96eee&oe=58CE5B13";
        };
    };
})

我的要求是将这些电子邮件、姓名、ID 字段分配给标签并打印出来。

我的相关视图控制器的代码如下。我应该添加什么??

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController,FBSDKLoginButtonDelegate{

    @IBOutlet weak var displayLabel: UILabel!
    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        let loginButton = FBSDKLoginButton()
        view.addSubview(loginButton)
        //frame's are obselete, please use constraints instead because its 2016 after all
        loginButton.frame = CGRect(x: 16, y: 50, width: view.frame.width - 32, height: 50)

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

    }
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Did log out of facebook")
    }

  @IBAction func loginButtonPressed(_ sender: AnyObject) {
        FBSDKLoginManager().logIn(withReadPermissions: ["email"], from: self) { (result, err) in
            if err != nil {
                print("Custom FB Login failed:", err)
                return
            }
            print("FB login Sucess")
            print(result?.token.tokenString)



            self.showEmailAddress()
        }
    }

    func showEmailAddress() {
        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email,picture"]).start { (connection, result, err) in

            if err != nil {
                print("Failed to start graph request:", err)
                return
            }
            print(result)

//            self.displayLabel.text = result["email"]as? NSMutableArray
        }
    }

}

【问题讨论】:

  • result["email"]as? NSMutableArray 为什么是 NSMutableArray? as String,不是吗?
  • 尽管你放了什么,但它给出了一条错误消息,说“Type Any 没有订阅成员”。这就是为什么我评论了那条线先生。只是想将该结果分配给标签,因为我已经尝试过但失败了
  • 你必须告诉编译器,result 是一个字典(在我们的例子中:[String:Any])。

标签: ios swift dictionary swift3 facebook-sdk-4.x


【解决方案1】:

首先将结果对象的类型指定为[String: Any],然后从中获取值。

if let dic = result as? [String: Any] {
    self.displayLabel.text = dic["email"] as? String
    self.displayNameLabel.text = dic["name"] as? String
    self.displayIdLabel.text = dic["id"] as? String
}

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2011-10-25
    • 2019-04-25
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    相关资源
    最近更新 更多