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

前端之家收集整理的这篇文章主要介绍了无法使用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
            }         
        })

    }

猜你在找的Swift相关文章