Facebook login dialog will not work with UIAlertView, but does with UIButton
I have this piece of code whi开发者_JAVA百科ch I use for showing a login dialog/posting to the users wall:
_posting = YES;
// If we're not logged in, log in first...
if (![_session isConnected]) {
self.loginDialog = nil;
loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
// If we have a session and a name, post to the wall!
else if (_facebookName != nil) {
[self postToWall];
}
// Otherwise, we don't have a name yet, just wait for that to come through.
If I connect this code to a button, it works. However, when I place it inside the following method:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(@"No");
}
else
{
_posting = YES;
// If we're not logged in, log in first...
if (![_session isConnected]) {
self.loginDialog = nil;
_loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
// If we have a session and a name, post to the wall!
else if (_facebookName != nil) {
[self postToWall];
}
// Otherwise, we don't have a name yet, just wait for that to come through.
}
}
It doesn't work..
Can anyone explain as to why this might be? When I say it doesn't work, I mean the dialog displays very briefly before closing itself. The app doesn't crash.
The waiting 1 sec is not a solution and doesn't actually fixes the problem.
The problem is that you listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Which should be:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
Because at the clickedButtonAtIndex
function the view isn't closed and this can cause problems.
Edit: To make it work in iOS 4 add your facebook login call to a method and call the following function in the didDismissWithButtonIndex:
[self performSelectorOnMainThread:@selector(facebookOpenAction) withObject:nil waitUntilDone:NO];
Figured it out, using the following line in the conditional instead of the facebook dialog code:
[self performSelector:@selector(showFBLoginDialog) withObject:nil afterDelay:1];
Seems you need to wait for the resources associated with the alert view to become available.
EDIT: The showFBLoginDialog method contains the dialog code that was originally in the conditional.
This is no longer an issue in iOS 5.
I don't see this as a solution, rather a quick fix/hack. Waiting for a 1 second delay may seem a little long, I wonder how low this delay can be set, the simulator shouldn't be used to asses this, and I no longer have a device running iOS 4. Would be interested to find out.
Maybe it would be better to add KVO to the alert view object to have a more precise idea of when it is destroyed, and trigger the dialog at that point.
精彩评论