没有获得FBSDKtoken

时间:2017-10-02 10:06:03

标签: ios swift swift3

echo "<pre>";print_r($day);echo "</pre>";

我尝试了这段代码但没有得到任何回复,收到错误:

  

在解包可选值时意外发现nil

我没有得到FBSDKtoken。

2 个答案:

答案 0 :(得分:1)

let loginManager = FBSDKLoginManager()
        loginManager.logIn(withReadPermissions: ["user_about_me", "email" , "user_birthday","user_hometown"], from: self) { (loginResult, error) in
            if error != nil
            {

            }
            else
            {
                if loginResult?.grantedPermissions == nil
                {

                    return
                }


                if (loginResult?.grantedPermissions.contains("email"))!
                {
                    if (loginResult?.grantedPermissions.contains("user_birthday"))!
                    {
                        self.getFBUserData()
                    }

                }
                }}

    }
    func getFBUserData()
    {

        FBSDKGraphRequest.init(graphPath: "me?fields=id,name,email,first_name,last_name,cover,picture.type(large),gender,birthday,hometown", parameters: nil).start(completionHandler: { (connection , result , error ) in

            if(error == nil){



                DispatchQueue.main.async {
                    let dictionary = result as! NSDictionary

                    print(dictionary)
                    print("Name : \(dictionary.value(forKey: "name")!)")
                    print("FB ID : \(dictionary.value(forKey: "id")!)")


                }


            }else{

                self.showalert(strMessage: "Somthig Went Wrong..!")
            }
        })

    }

在Swift-3 TryThis

答案 1 :(得分:0)

尝试使用以下代码

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

 @IBAction func btnFbSignInAction(_ sender: Any) {

        fbLoginManager.logOut()
        fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
            if error != nil {
                // Handle Error
                NSLog("Process error")
            } else if (result?.isCancelled)! {
                // If process is cancel
                NSLog("Cancelled")
            }
            else {
                // Parameters for Graph Request
                let parameters = ["fields": "email, name"]

                FBSDKGraphRequest(graphPath: "me", parameters: parameters).start {(connection, result, error) -> Void in
                    if error != nil {
                        NSLog(error.debugDescription)
                        return
                    }

                    // Result
                    print("Result: \(result)")

                    // Handle vars
                    if let result = result as? [String:String],
                        let email: String = result["email"],
                        let fbId: String = result["id"] {
                        print("Email: \(email)")
                        print("fbID: \(fbId)")
                    }
                }
            }
        }
    }