ios - FacebookSDK如何重新提示用户同意?

标签 ios swift facebook-sdk-4.x

我想使用 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()
        }
    }
}

最佳答案

我在提供的片段中遇到了一些问题,我将逐步解决这些问题。修改了底部的代码。

编译错误

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

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

use of unresolved identifier 'loginManager'

从外观上看,您已经在 View Controller 上保存了一个可选的 FBSDKLoginManager,但这不是必需的,并且会打乱您重新提示用户输入电子邮件的尝试。

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

(不幸的是,“重新请求”是挑剔和隐含的......我从这篇文章中学到了我所知道的一切,这并不多-How to “rerequest” email permission using Facebook iOS SDK 4.x?)

时间

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

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

其他

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

    self.dismissViewControllerAnimated(true, completion: nil)

修改后的代码

//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
        }
    }
}

关于ios - FacebookSDK如何重新提示用户同意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808659/

相关文章:

ios - 如何设置与2个坐标距离相关的 map 套件跨度和区域?

ios - Cordova iOS : xcrun can't find "PackageApplication" after xcode update

ios - 添加多个单元格时 UICollectionViewCell 未正确对齐

ios - 是否可以将数据扔到另一个 View Controller ?

facebook-ios-sdk - iOS Facebook 应用邀请 : error

android - FB Analytics 推送 Activity - "Set up Your Push Campaigns"在实现后仍然存在

ios - 使用 UITouchTypeStylus 处理 Apple Pencil 触摸事件

ios - 如何在 iOS 中动态更改 UICollectionView 单元格高度

swift - 苹果自己的 Swift 文档关于动态类型与编译时类型似乎是错误的

android - :app:transformClassesWithDexForDebug while compiling Firebase