Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...