Facebook Connect Dialog on iPhone

A problem I keep having when adding Facebook Connect to iPhone applications is that sometimes the dialogs appear briefly then disappear. I keep forgetting why it is for a while, so I thought I’d post this to help anyone who is having a similar problem, and to remind myself about it so that maybe next time I won’t forget 🙂

Anyway, the problem is in the FBDialog.m file. There are a couple of lines that read:

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

The problem comes when you try and show the Facebook dialog just after something other than the main window is the key window. This could happen if you’ve popped up a UIAlertView to ask if the user wants to publish to Facebook. The UIAlertView is the application’s key window and when you display your Facebook dialog, it adds itself as a subview of the UIAlertView (which is currently closing). So, your dialog will start to display, then when the UIAlertView finishes closing, it’ll disappear. The easy fix is to just change the FBDialog.m file so that it always uses the second line, and doesn’t try to use the keyWindow property:

UIWindow* window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[window addSubview:self];