无法使用swift 3和ios 10从facebook检索电子邮件

您好我正试图从Facebook检索我的电子邮件,因为我正在使用 swift播放facebook ios sdk. IOS平台是10,swift 3和 Xcode 8.我在线跟踪教程,但无法检索电子邮件.

下面是我的代码

if FBSDKAccesstoken.current() == nil {
            print("I got token")
            let fbButton = FBSDKLoginButton()
            fbButton.readPermissions = ["public_profile","email","user_friends"]
            view.addSubview(fbButton)
            fbButton.center = view.center
            fbButton.delegate = self
            self.fetchprofile()
        }

        else {
            print("Dont have token")
            let loginView : FBSDKLoginButton = FBSDKLoginButton()
            self.view.addSubview(loginView)
            loginView.center = self.view.center
            loginView.readPermissions = ["public_profile","user_friends"]
            loginView.delegate = self
        }

func loginButton(_ loginButton: FBSDKLoginButton!,didCompleteWith result: FBSDKLoginManagerLoginResult!,error: Error!) {
        if error != nil {
            print(error.localizedDescription)
            return
        }
        print("I'm in")
        fetchprofile()
    }

func fetchprofile() {

        print("Getting profile")

        let parameters = ["fields": "email"]

        let graphRequest:FBSDKGraphRequest = FBSDKGraphRequest(graPHPath: "me",parameters: parameters,httpMethod: "GET")

        graphRequest.start(completionHandler: {(connection,result,error) -> Void in

            if error != nil {
                print("Error retrieving details: \(error?.localizedDescription)")
                return
            }

            guard let result = result as? [String:[AnyObject]],let email = result["email"] else {
                return
            }
            print("Email: \(email)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
            self.view.backgroundColor = UIColor.red

        })
    }

在我的appdelegate.swift文件中:

//have both google and facebook signin. Google works but facebook doesn't
func application(_ app: UIApplication,open url: URL,options: [UIApplicationopenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url,sourceApplication: options[UIApplicationopenURLOptionsKey.sourceApplication] as! String,annotation: options[UIApplicationopenURLOptionsKey.annotation]) ||
        FBSDKApplicationDelegate.sharedInstance().application(app,open: url,annotation: options[UIApplicationopenURLOptionsKey.annotation])
    }

我能够登录并注销但无法检索电子邮件.

更新实际上,当我实际传递打印(电子邮件)时,我可以在控制台上看到它作为可选语句.我在没有可选参数的情况下显示它时遇到了麻烦

我用这种方式解决了这个问题:
func fetchProfile(){
     FBSDKGraphRequest(graPHPath: "/me",parameters: ["fields" : "email,name,id,gender"])
        .start(completionHandler:  { (connection,error) in
            guard let result = result as? NSDictionary,let email = result["email"] as? String,let user_name = result["name"] as? String,let user_gender = result["gender"] as? String,let user_id_fb = result["id"]  as? String else {
                    return
            }         
        })

    }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...