PFFacebookUtils login methods do nothing if Facebo

2019-09-10 04:42发布

This is very weird. I'm trying to login to Facebook and everything is working fine if there's no Facebook app is installed on the device. But if there's this app then nothing happens. Just nothing. Completion block is never called and no login dialog appears. This is my very simple code:

[PFFacebookUtils logInInBackgroundWithReadPermissions:@[@"public_profile"] block:^(PFUser *user, NSError *error)
 {
     NSLog(@"Completion");

 }];

I also have this code inside applicationDidFinishLauncing:

[Parse setApplicationId:kPAParseApplicationId clientKey:kPAParseClientKey];    
[PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions];

and I have the following method implemented:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                   openURL:url
                                         sourceApplication:sourceApplication
                                                annotation:annotation];

}

I also need to note the following: sometimes, very rarely, it works even with the app installed. Works and then again stops working without any reason. Did anyone encounter such a problem? Thanks!

1条回答
Root(大扎)
2楼-- · 2019-09-10 05:29

I've figured it out. The problem was in the Facebook SDK itself and I've managed to fix it. It has nothing to do with Facebook app being installed or not.

Luckily, FBSDKLoginKit and FBSDKCoreKit are open source so I was able to track down the problem. So, FBSDKLoginManager allows login only if it has an active SDK configuration loaded from Facebook server. If it doesn't then it simply do nothing without even logging some error message to help the developer. So what is the reason it doesn't have active configuration sometimes. The reason is that FBSDKURLConnection doesn't schedule its NSURLConnection on the main run loop. So if its start method is called on background thread then NSURLConnection delegate methods may be called or they may be not called. And if they are not then there's no new configuration loaded and you are unable to login. So what I did was simply modify FBSDKURLConnection's start method like this:

- (void)start
{
    [_connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
    [_connection start];
}

and now it works

查看更多
登录 后发表回答