Ios 如何使用FacebookSDK重新提示用户的权限?

Ios 如何使用FacebookSDK重新提示用户的权限?,ios,swift,facebook-sdk-4.x,Ios,Swift,Facebook Sdk 4.x,我想使用FacebookSDK实现功能 作为示例应用程序,您可以检查url: 我已经写了这段代码,但它并没有像预期的那样为我工作 //Callback function for default FBLogin Button func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { prin

我想使用FacebookSDK实现功能

作为示例应用程序,您可以检查url:

我已经写了这段代码,但它并没有像预期的那样为我工作

//Callback function for default FBLogin Button
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
    print("User Logged In")

    if (error != nil)
    {
        // Process error
        print("Processing Error : \(error)")
        FBSDKLoginManager().logOut()
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    else if result.isCancelled
    {
        // Handle cancellations
        print("user is cancelled the login FB")
        FBSDKLoginManager().logOut()
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    else
    {
        print("result : \(result)")

        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.declinedPermissions.contains("email")
        {
            print("email is declined")
            // Do work
            loginManager = FBSDKLoginManager()
            loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in

                    if error == nil
                    {
                        self.fetchUserData()
                    }

                })
        }
        else
        {
            var readPermissions : FBSDKLoginManagerLoginResult = result
            Constants.isUserLoggedIn = true
            fetchUserData()
        }
    }
}

我在提供的代码片段中遇到了一些问题,我将详细介绍这些问题。修改了底部的代码

编译错误

当我试图按照给定的方式运行代码时,我得到一个编译错误

loginManager = FBSDKLoginManager()
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in
使用未解析的标识符“loginManager”

从外观上看,您已经在视图控制器上保存了一个可选的FBSDKLoginManager,但这并不是必需的,并且会打乱您重新向用户发送电子邮件的尝试

他们不会给你第二次访问电子邮件的机会,只会看到“你已经授权[此处的应用程序名称]”对话框

(不幸的是,“重新请求”很挑剔而且含蓄……我从这篇文章中学到了我所知道的一切,但并不多。)

定时

另一个主要问题似乎只是关于您调用以重新请求权限的时间。当我运行你的代码,不检查电子邮件访问,我看到一个空白的Facebook弹出窗口

然而,正如在示例应用程序中一样,当我将重新提示包装在一个对话框中,解释我需要电子邮件的目的时,我看到了我期待的重新提示

其他

  • 将错误处理添加到您的重发尝试中(否则您将遇到强制展开零错误)
  • 删除了不必要的呼叫 self.dismissViewControllerAnimated(真,完成:无)

修订代码

//Callback function for default FBLogin Button
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
    print("User Logged In")

    if (error != nil)
    {
        // Process error
        print("Processing Error : \(error)")
        FBSDKLoginManager().logOut()
    }
    else if result.isCancelled
    {
        // Handle cancellations
        print("user is cancelled the login FB")
        FBSDKLoginManager().logOut()
    }
    else //permissions were granted, but still need to check which ones
    {
        if result.declinedPermissions.contains("email")
        {
            let alert = UIAlertController(title: "Alert", message: "We need your email address to proceed", preferredStyle: UIAlertControllerStyle.Alert)
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { action in
                // Handle cancellations
                print("user is cancelled the login FB")
                FBSDKLoginManager().logOut()

            })
            let reRequestAction = UIAlertAction(title: "Grant Access", style: UIAlertActionStyle.Default, handler: { action in
                let fbsdklm = FBSDKLoginManager()
                fbsdklm.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
                    if (error != nil)
                    {
                        // Process error
                        print("Processing Error : \(error)")
                        FBSDKLoginManager().logOut()
                    }
                    else if result.isCancelled {
                        // Handle cancellations
                        print("user is cancelled the login FB")
                        FBSDKLoginManager().logOut()
                    }
                    else {
                        print("Got Email Permissions!")
                        //proceed
                    }
                }
            })

            alert.addAction(cancelAction)
            alert.addAction(reRequestAction)
            self.presentViewController(alert, animated: true, completion: nil)

        }
        else
        {
            print("Got Email Permissions!")
            //proceed
        }
    }
}

在拒绝某些权限后调用logInWithReadPermissions时,执行完全相同的操作。我在适当的警报后调用此方法,该警报会告诉我为什么需要权限,然后它会直接将我带到登录屏幕而不是权限对话框,那么您能告诉我为什么会发生这种情况吗?