iOS: Connect to Facebook without leaving the app f

2019-01-17 05:40发布

问题:

I know that it was possible before the Graph API.

I work on an iPhone app that may not be in the background (one of the requirements).
In addition there is a login screen on the app launching.
Therefore it is not suitable to go to background in order to authenticate to Facebook and then return to the app and login once again each time the user wants to share something.

So, my question is if there is a way to authenticate with Facebook without leaving the app.

BTW, I have tried to use the old API.
It worked in the beginning but yesterday it stopped working.
I just see a blank screen inside the old Facebook login web view.
I have also checked one of my old apps that use that old Facebook Connect API and I get the same blank login screen in that app too.

Any idea will be appreciated.

Thank you in advance.

--
Michael.

回答1:

In Facebook.m

- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth {
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _appId, @"client_id",
                                 @"user_agent", @"type",
                                 kRedirectURL, @"redirect_uri",
                                 @"touch", @"display",
                                 kSDKVersion, @"sdk",
                                 nil];

method comment out this

 UIDevice *device = [UIDevice currentDevice];
  if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
    if (tryFBAppAuth) {
      NSString *fbAppUrl = [FBRequest serializeURL:kFBAppAuthURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }

    if (trySafariAuth && !didOpenOtherApp) {
      NSString *nextUrl = [NSString stringWithFormat:@"fb%@://authorize", _appId];
      [params setValue:nextUrl forKey:@"redirect_uri"];

      NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
    }
  }

This will prevent the app from going to background and show you the standard fb dialog.



回答2:

Here you have an alternative solution.

If you don't like to change the facebook sdk code and want a solution that allows you to choose between SSO or the old-fashioned mechanism, you can Implement an extension like this:

//Facebook_SSOExtension.h
--------------------------------------------------------

@interface Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO;
@end

//Facebook_SSOExtension.m
--------------------------------------------------------

//So warnings do not appear
@interface Facebook(PrivateSSOExtension)
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth;
-(void) setPermissions:(NSArray*) permissions;
@end 

@implementation Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO
{
    [self setPermissions: permissions];
    [self authorizeWithFBAppAuth:useSSO safariAuth:useSSO];
}
@end

Even though this requires more work than commenting-out the sso code, you will be able to update the facebook-sdk without major problems (if they rename authorizeWithFBAppAuth:safariAuth: your extension will not work, use asserts to detect this issue quickly). Also, if you are building a reusable component to interact with facebook without repeating things over and over again, this will save you some work too.

Cheers.