开发者

facebook dialog failing silently when not called immediately after authorize

I have the following function to post to Facebook using the latest iOS Facebook SDK.

-(void)fbPost:(NSMutableDictionary *) params{
    NSLog(@"fbPost called");

    if (![facebook isSessionValid]) {
        NSLog(@"session invalid, calling fblogin");

        [self fblogin];
开发者_开发技巧    }
    if ([facebook isSessionValid]) {
        NSLog(@"session valid, calling publishToFB");

        [self.facebook dialog:@"stream.publish" andParams:params andDelegate:self];

    }

}

It works fine when there is no existing session: it logs in to facebook, gets permissions, returns to the app, shows the dialog and publishes the status. However, when trying a second time, isSessionValid returns true the first time and nothing happens, although the log shows publishToFB is called.

The session is persisted in fbDidLogin:

[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:@"AccessToken"];
    [[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:@"ExpirationDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];

and loaded in application didFinishLaunchingWithOptions:

facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];

I made sure to ask for offline_access permission when logging in:

_permissions =  [[NSArray arrayWithObjects:
                      @"publish_stream",@"offline_access",nil] retain];


It appears the problem was in drawing the dialog in this code in FBDialog.m

UIWindow* window = [UIApplication sharedApplication].keyWindow;
  if (!window) {
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
  }

If I comment out the If clause, it works OK. I guess that in my app setup, "keyWindow" is not front-most, so the dialog was not showing up.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜