开发者

Problems with fbDidLogin never called iOS

I'm having problems with my app saving the access_token. The problem is, when an iPhone hasn't the Facebook app installed, safari will handle this and there is no problem. But as soon as the iPhone has the Facebook app installed. It Just isn't saving the access token and opens the Facebook app every time when it needs to do something with Facebook. It then says, you already have given .... permission...

This is what i'm doing to in the viewdidload to ask permission the first time and receive the access token from the user defaults:

facebook = [[Facebook alloc] initWithAppId:@"APPID"];


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.exp开发者_开发技巧irationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    NSArray* permissions =  [[NSArray arrayWithObjects:
                              @"read_stream", @"offline_access", nil] retain];

    if (![facebook isSessionValid]) {
        [facebook authorize:permissions delegate:self];
    }

This is what i'm doing in the fbdidlogin:

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];   
}

But for some way when the Facebook app takes control this just doesn't work...It wil doe authorize every time when the view did load, loads again.

Has anyone got a clue?

Thnx!

btw: I also have the following line:

(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [[controller facebook] handleOpenURL:url]; }


I just had this problem and believe I may have found your solution. When you create the app on facebook you must edit your app settings to allow for native ios app. The ios bundle id selected must match EXACTLY to your bundle ID in the app you are creating. Facebook states that they check this prior to authorizing your app. Funny thing is if you remove the facebook app from the device and then try to authenticate it uses the web browser and seemingly bipasses this "security" completely. This is why Jos solution "works". Facebook doesn't send an error message back so it was a lot of fun to track down. I figured I'd throw this answer out there in hopes to save some future devs some trouble. I hope this helps.


Have you disabled multi-task on your app via Info.plist? The Facebook SDK makes the (undocumented assumption) that your app supports multi-task.

By that I mean you have a "Application does not run in background" (UIApplicationExitsOnSuspend) set to YES.

If that is the case, you're going to have to use login via an in-app browser for which I will update my post and provide a code sample.


I've found a workaround, which imo is better the using the Facebook app. In the Facebook.m file i've changed this:

  [self authorizeWithFBAppAuth:YES safariAuth:YES];

to this

  [self authorizeWithFBAppAuth:NO safariAuth:YES]; 

In this case users don't leave the app, and everything is handled within the popup without using the Facebook app.


That's what it does now. It bails out to the Facebook app if it's installed, or to Safari if it's not. You can explicitly use the dialog interface, but the simplest sample code supplied with the library does its auth business by leaving your app, then coming back after the FB app has handled auth. It works okay, and once you have your head around it (and have it configured properly to return to you), it's fine.

To prevent re-authentication on each session, request the permission token "offline_access".


Double check your controller reference in the handleOpenURL: method on your app delegate. I get the feeling that it may be setting to nil in the background, so Facebook never receives the response.


The lines: [self authorizeWithFBAppAuth:YES safariAuth:YES];

being changed to

[self authorizeWithFBAppAuth:NO safariAuth:YES];

is possible only when we have access to Facebook.m (since that's where one would have to make a change)

In case we're using Facebook's latest static library, we don't get access to the the .m file. In that case how would we be able to achieve the same functionality?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜