微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用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
            }         
        })

    }

原文地址:https://www.jb51.cc/swift/318705.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐