加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:40:07 所属栏目:百科 来源:网络整理
导读:您好我正试图从Facebook检索我的电子邮件,因为我正在使用 swift播放facebook ios sdk. IOS平台是10,swift 3和 Xcode 8.我在线跟踪教程,但无法检索电子邮件. 下面是我的代码: if FBSDKAccessToken.current() == nil { print("I got token") let fbButton = F
您好我正试图从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)nnnnnnnnnnnnnnn")
            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
            }         
        })

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读