iOS: Connect to Facebook without leaving the app for authorization

后端 未结 2 2027
栀梦
栀梦 2020-12-08 12:19

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 log

相关标签:
2条回答
  • 2020-12-08 13:08

    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.

    0 讨论(0)
  • 2020-12-08 13:15

    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.

    0 讨论(0)
提交回复
热议问题