2

I'm trying to integrate an iOS application with Facebook. Login and most of the features work well but I am experiencing a little-big problem when I try to get FBWebDialogs to share something that user has posted. It works well if it is called directly just after posting (calling the function) but crashes when I try to do the same from an alert view, it just starts to appear and suddenly disappears before loading completely without breaking the app and no error is thrown. It works using new Facebook features in iOS 6 that has native view instead FBWebDialogs when user has logged within iPhone's settings. I'm using a third party library in order to customize alert view but it does the same with standard alert view.

I think it is something to be with dismissing view but not enough knowledge about iOS to know for sure. Any idea?. Thank you.

That's the code:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    switch (buttonIndex)
    {
        case 1:
        {
            [self sharePostOnFacebook];
            break;
        }
        default:
            break;
    }
}

The next method is called but modal view never comes to appear completely. It just flashes and disappears

-(void) sharePostOnFacebook
{
    if (shareData !=nil && [[shareData objectForKey:@"success"] boolValue])
    {
        NSString *caption = [shareData objectForKey:@"caption"];
        . ..........


        BOOL displayedNativeDialog =
        [FBNativeDialogs
         presentShareDialogModallyFrom:self
         initialText:description
         image:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:picture]]]
         url:[NSURL URLWithString:link]
         handler:^(FBNativeDialogResult result, NSError *error) {

             .........
         }];
        // Fallback, show the view controller that will post using me/feed
        if (!displayedNativeDialog)
        {
            [[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];

            // Put together the dialog parameters
            NSMutableDictionary *params =
            [NSMutableDictionary dictionaryWithObjectsAndKeys:
             .............
             nil];

            FBSession *activeSession = [FBSession activeSession];
            // Invoke the dialog
            [FBWebDialogs presentFeedDialogModallyWithSession:activeSession
                                                   parameters:params
                                                      handler:
             ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                ........
             }];
        }

    }

}
Kara
  • 6,115
  • 16
  • 50
  • 57
bitsdisasters
  • 253
  • 5
  • 14

2 Answers2

3

Here's the solution I settled with (https://developers.facebook.com/docs/howtos/send-requests-using-ios-sdk/).

The key seems to be delaying the call to FBWebDialog.

[self performSelector:@selector(sendRequest) withObject:nil afterDelay:0.5];

And sendRequest can just be

- (void)sendFlyerFloRequestToFriends
{
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
   [FBWebDialogs presentRequestsDialogModallyWithSession: ...

If presentRequestsDialogModallyWithSession is called with performSelector (using a delay), then the crashing problem seems to disappear - not sure why, but seems to work on my end.

markhops
  • 333
  • 5
  • 11
0

Late answer.. but for the record.. I've fixed it using:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;

instead of:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

I believe the problem is related to the FBWebDialog being presented while the UIAlertView is still being shown. Using 'didDismiss' makes sure the Alert is already gone when the webdialog is presented.

manecosta
  • 8,682
  • 3
  • 26
  • 39