6

I understand - http://developers.facebook.com/docs/guides/mobile/#ios -, seems really useful, but my app won't support multitasking, so this "cross safari" method won't work for me.

Is there a brief example somewhere (or can someone please copy here) an authentication process trough FBDialogs (with this so called "new" API)? I'm just getting know this SDK, so please as basic as you can.

atamanroman
  • 11,607
  • 7
  • 57
  • 81
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • 4
    You should accept some answers so that people are encouraged to help you out more. – Moshe Apr 05 '11 at 22:12
  • if the device doesn't support multitasking... then *old* Dialog method will be used. that's how that sdk works! – Vincent Guerci Apr 16 '11 at 00:00
  • 1
    I cannot emphasise more on what @Moshe said "You should accept some answers so that people are encouraged to help you out more.", and you seem to have got the idea but never replied to anyone answering your tens of questions. – Khiet May 06 '11 at 22:34

2 Answers2

10

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

Khiet
  • 872
  • 1
  • 10
  • 15
8

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...

Vincent Guerci
  • 14,379
  • 4
  • 50
  • 56
  • This worked. Thanks! One alternative would be to check the launchOtions (application:didFinishLaunchingWithOptions:). The options would contain the launch URL including the tokens so they could be assigned either to the NSUserDefaults (if present of course) or directly to the facebook.accessToken and facebook.expirationDate fields. – Oli Aug 03 '11 at 22:10
  • Will Apple reject this, since we are trying to use a Private API ? – Illep Mar 10 '12 at 17:31
  • @Illep This is Facebook code, not Apple one... Nothing is private, or even an api, and not from Apple. This answer might be obsolete with latest facebook sdk btw... – Vincent Guerci Mar 10 '12 at 19:57
  • @VincentG Are you able to expand on your suggested code? I'm unclear how to use the category you've mentioned. – Jimmy Jun 26 '12 at 16:53