Facebook login dialog not working on iOS6 simulator

I converted my application from the old Facebook SDK to the new Facebook SDK 3.1 in order to use iOS6's own functionality, as well as other Facebook features, and I have a problem that constantly repeats on the simulator and rarely on the Device.

When I go to Facebook on Facebook, instead of connecting to applications and checking them through Safari or the Facebook application, I get a modal window on top of my login application (I believe it started with iOS6, even before switching to Facebook SDK 3.0 /3.1). This window allows me to enter a username and password, but when I go to click "OK" in the upper right corner for authentication, nothing happens. The callback does not apply to the application: openURL: sourceApplication: annotation: in AppDelegate.

Facebook Login

In my common FacebookController code (which I use for several projects), I initialize FBSession when I try to log in according to the Facebook code sample:

FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:readPermissions defaultAudience:FBSessionDefaultAudienceNone urlSchemeSuffix:self.fbUrlSchemeSuffix tokenCacheStrategy:nil]; if(session.state == FBSessionStateCreated || session.state == FBSessionStateCreatedTokenLoaded) { [FBSession setActiveSession:session]; // we open after the fact, in order to avoid overlapping close // and open handler calls for blocks [session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { ... 

The completion handler code never executes unless I click the x cancel button in the dialog box (in this case, I get an error as expected).

I set FacebookAppID in my iOS target properties (info.plist) and I have URL schemes configured with fb ### [url_scheme_suffix].

The only difference I have between my code and the Facebook code sample is that I use URL scheme suffixes, as I have a free and paid version of our application.

If the application uses Safari to log in, it works with the specified code. If he quickly switches to the Facebook app, it also works. In addition, the general settings of the facebook ios6 account work. An application works only if it is used in a modal application.

Has anyone else come across this?

+6
source share
1 answer

The same thing happens to me. This only happens on the simulator (iphone5 4 inch, ios6), so I don’t know if this problem will be present in real devices.

After several debugging sessions, I found that the FBDialog webView: shouldStartLoadWithRequest: navigationType: method did not process the URL at which access_token was. When facebook passes access_token, it means that you are authenticated. So, I thought something was missing there.

Here is the URL form that is not being processed:

fb12222222222222222 : // allow / # access_token = ... & expires_in = 5102464 & code = ...

Note that the URL scheme is the same fb + API key that you configured in the Info.plist file in your application.

Here is what I did to fix this problem. Note that I added a new else-if branch in which I invoke the success of the dialog if we have an access token:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSURL* url = request.URL; if ([url.scheme isEqualToString:@"fbconnect"]) { if ([[url.resourceSpecifier substringToIndex:8] isEqualToString:@"//cancel"]) { NSString * errorCode = [self getStringFromUrl:[url absoluteString] needle:@"error_code="]; NSString * errorStr = [self getStringFromUrl:[url absoluteString] needle:@"error_msg="]; if (errorCode) { NSDictionary * errorData = [NSDictionary dictionaryWithObject:errorStr forKey:@"error_msg"]; NSError * error = [NSError errorWithDomain:@"facebookErrDomain" code:[errorCode intValue] userInfo:errorData]; [self dismissWithError:error animated:YES]; } else { [self dialogDidCancel:url]; } } else { if (_frictionlessSettings.enabled) { [self dialogSuccessHandleFrictionlessResponses:url]; } [self dialogDidSucceed:url]; } return NO; } //THIS IS WHAT I'VE ADDED>>>> else if ([[[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleURLTypes"] firstObject] objectForKey:@"CFBundleURLSchemes"] firstObject] isEqual:url.scheme]) { [self dialogDidSucceed:url]; return NO; } //<<<<< else if ([_loadingURL isEqual:url]) { return YES; } else if (navigationType == UIWebViewNavigationTypeLinkClicked) { if ([_delegate respondsToSelector:@selector(dialog:shouldOpenURLInExternalBrowser:)]) { if (![_delegate dialog:self shouldOpenURLInExternalBrowser:url]) { return NO; } } [[UIApplication sharedApplication] openURL:request.URL]; return NO; } else { return YES; } } 

I should mention that I follow all the recommendations of fb sdk (3.1), and I did not make any further changes to the src code.

With these changes, the fb registration process works as expected.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/926863/


All Articles