How to skip Facebook's permission page when logging in on a device that a user is already logged into Facebook

时间:2015-05-04 19:34:49

标签: ios facebook login

I'm writing an iOS8 application in Swift. I'm trying to login using Facebook and I'm using Facebook iOS SDK 4.1. I'm not using the Facebook button. I have my own simple button. Clicking on the button, calls facebookLogin method that I wrote and am pasting below. I run the app on a computer that I'm already logged in on Facebook. For the first run it asks for the permissions and after I press OK, it logs in and everything's fine. On the later runs, when I'm still logged in on the same computer, the application again shows the permission page, this time saying that the user already has authorized the app and gives an OK and Cancel option. Pressing Ok, proceeds with the login. I'm attaching the screenshot below. My problem is that I don't want to get the permission page on these later runs. I assume that it should silently proceed without bouncing to the Facebook app/website. Trying it on the phone has similar behavior. It doesn't show the permission page, but the bouncing to the Facebook app occurs and it comes back almost immediately.

let fbLoginManager = FBSDKLoginManager()

func facebookLogin() {

    var token = FBSDKAccessToken.currentAccessToken()
    if (token != nil) {
        return
    }

    fbLoginManager.logInWithReadPermissions(["public_profile"], handler: { (result: FBSDKLoginManagerLoginResult!, error: NSError!) -> Void in
        if ((error) != nil) {
            // Process error
        } else if (result.isCancelled) {
            // Handle cancellations
        } else {
            if (result.grantedPermissions.contains("public_profile"))  {
                // Handle the result
            }
        }
    })
}

And here's the relevant section in AppDelegate:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

I would appreciate your help. I've seen numerous posts on this subject but not much help, specifically on the new Facebook SDK. Thanks!

enter image description here

2 个答案:

答案 0 :(得分:1)

You should check for a valid FBSDKAccessToken before calling logInWithReadPermissions.

if FBSDKAccessToken.currentAccessToken != nil
{
  // already logged in with the requested permissions
}
else
{
  // login here
}

Keep in mind that if you decide to ask for some new permissions, you should probably check for those permissions specifically by checking the permissions property (NSSet) of the access token rather than just checking for the presence of a token.

答案 1 :(得分:0)

(这最初是我的评论,但结果也是解决方案)

你需要确保你正在调用(Swift相当于)以下内容:

return [[FBSDKApplicationDelegate sharedInstance] 
         application:application 
         didFinishLaunchingWithOptions:launchOptions];

这需要SDK正确启动。这在此处记录:

https://developers.facebook.com/docs/reference/ios/current/class/FBSDKApplicationDelegate/#instance_methods

  

从[UIApplicationDelegate]调用此方法   application:didFinishLaunchingWithOptions:] AppDelegate的方法   为您的应用程序。应该调用它来正确使用Facebook   SDK。