How to authorize user through a DIALOG with the NEW Facebook Connect iOS API?

孤街浪徒 提交于 2019-11-27 13:22:41

Struggled to figure this out for a couple of hours...

Here is the solution to bypass fb app|background-safari authentication, and it is just to change in Facebook.m:

[self authorizeWithFBAppAuth:YES safariAuth:YES]

to

[self authorizeWithFBAppAuth:NO safariAuth:NO]

in the method:

- (void)authorize:(NSArray *)permissions
     delegate:(id<FBSessionDelegate>)delegate

.

Verified it with the sample code without changing anything else (of course set kAppId to yours)

I was quite mad why on earth FB doesn't document this...

Hope it helps,

Cheers

All the magic is in

- (void)authorize:(NSArray *)permissions
     delegate:(id<FBSessionDelegate>)delegate

which call [self authorizeWithFBAppAuth:YES safariAuth:YES] :

  • FBAppAuth:YES will try to authenticate using Facebook app if installed
  • safariAuth:YES will try to authenticate using Safari if device supports multitasking

So what you want is [self authorizeWithFBAppAuth:NO safariAuth:NO]

If you want to leave unmodified the Facebook SDK you can simply "expose" their private api :

@interface Facebook (Private)
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
                    safariAuth:(BOOL)trySafariAuth;
@end

And then extend if with a category:

@interface Facebook (MyApp)
- (void)myAuthorize:(id<FBSessionDelegate>)delegate;
@end

@implementation Facebook (MyApp)

- (void)myAuthorize:(id<FBSessionDelegate>)delegate {
  _permissions = [[NSArray arrayWithObjects:@"email", *(whatever you need)*, nil] retain];
  _sessionDelegate = delegate;
  [self authorizeWithFBAppAuth:NO safariAuth:NO]; // force in app auth
}

@end

Then use it almost normally :

 Facebook *facebook = [[Facebook alloc] initWithAppId:MY_APP_FB_ID];
 [facebook myAuthorize:self];

That's a popular request they didn't implemented, there are even pull requests with solutions...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!