1

我想使用 FacebookSDK 实现功能。

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

https://developers.facebook.com/docs/facebook-login/handling-declined-permissions#reprompt

我已经编写了这段代码,但它并没有像预期的那样对我有用。

//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()
        }
    }
}
4

1 回答 1

2

我在提供的代码段中遇到了一些问题,我将逐步介绍。修改后的代码在底部。

编译错误

当我尝试按照给定的方式运行您的代码时,出现编译错误

loginManager = FBSDKLoginManager()
loginManager!.logInWithReadPermissions(["email"], fromViewController: self, handler:{ [unowned self](result, error) -> Void in

使用未解析的标识符“loginManager”

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

他们不会再有机会让您访问电子邮件,而只会看到“您已经授权 [此处的应用程序名称]”对话框。

(不幸的是,“请求”是挑剔和隐含的......我从这篇文章中学到了我所知道的,这并不多 -如何使用 Facebook iOS SDK 4.x “重新请求”电子邮件权限?

定时

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

但是,当我在示例应用程序中将重新提示包装在一个对话框中并解释我需要电子邮件的用途时,我看到了我所期待的重新提示。

其他

  • 为您的重新提示尝试添加了错误处理(否则您会遇到强制解包 nil 错误)
  • 删除了不必要的调用

    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
        }
    }
}
于 2016-06-03T16:09:35.870 回答