presentRequestsDialogModallyWithSession does not work, but gives good result

Ruud Visser picture Ruud Visser · Mar 19, 2013 · Viewed 8.4k times · Source

When I use the webdialog for a friendrequest, everything is going fine, except no request or anything is made. The code:

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   facebookFriend.id, @"to",
                                   nil];
    [FBWebDialogs presentRequestsDialogModallyWithSession:FBSession.activeSession 
                                                  message:NSLocalizedString(@"FB_FRIEND_INVITE_MESSAGE", @"Facebook friend invite message")       
                                                    title:NSLocalizedString(@"FB_FRIEND_INVITE_TITLE", @"Facebook friend invite title") 
                                               parameters:params 
                                                  handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                                        }];

This is the result I get:

fbconnect://success?request=xxxxxxxxxxxx&to%5B0%5D=xxxxxxxx

How can I debug what is going wrong?

Thanks in advance.

Ruud

Answer

NeverHopeless picture NeverHopeless · May 20, 2013

For SDK 3.2 or above we have a facility to use FBWebDialogs class that will help us to show a popup along with the friend list and pick one or more from list to send invitation.

Lets do it step by step:

1) Download and setup SDK 3.2 or above.

2) First setup your application on facebook by following this url.

3) Then use the attached code.

Sample Code: (It generates invite friend request)

-(void)inviteFriends
{
    if ([[FBSession activeSession] isOpen])
    {
        NSMutableDictionary* params =  [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
       [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                      message:[self getInviteFriendMessage]
                                                        title:nil
                                                   parameters:params
                                                      handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
         {
             if (error)
             {
                 [self requestFailedWithError:error];
             }
             else
             {
                 if (result == FBWebDialogResultDialogNotCompleted)
                 {
                     [self requestFailedWithError:nil];
                 }
                 else if([[resultURL description] hasPrefix:@"fbconnect://success?request="]) 
                 {
                    // Facebook returns FBWebDialogResultDialogCompleted even user 
                    // presses "Cancel" button, so we differentiate it on the basis of
                    // url value, since it returns "Request" when we ACTUALLY
                    // completes Dialog
                     [self requestSucceeded];
                 }
                 else
                 {
                     // User Cancelled the dialog
                     [self requestFailedWithError:nil];
                 }
             }
         }
       ];

    }
    else
    {
        /*
         * open a new session with publish permission
         */
        [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_stream"]
                                           defaultAudience:FBSessionDefaultAudienceFriends
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
         {
             if (!error && status == FBSessionStateOpen)
             {
                 NSMutableDictionary* params =   [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
                 [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                               message:[self getInviteFriendMessage]
                                                                 title:nil
                                                            parameters:params
                                                               handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
                  {
                      if (error)
                      {
                           [self requestFailedWithError:error];
                      }
                      else
                      {
                          if (result == FBWebDialogResultDialogNotCompleted)
                          {
                              [self requestFailedWithError:nil];
                          }
                          else if([[resultURL description] hasPrefix:@"fbconnect://success?request="])
                          {
                              // Facebook returns FBWebDialogResultDialogCompleted even user 
                              // presses "Cancel" button, so we differentiate it on the basis of
                              // url value, since it returns "Request" when we ACTUALLY
                              // completes Dialog
                              [self requestSucceeded];
                          }
                          else
                          {
                              // User Cancelled the dialog
                              [self requestFailedWithError:nil];
                          }

                      }
                  }];
             }
             else
             {
                 [self requestFailedWithError:error];
             }
         }];
    }

}

and here are the helper functions that calls delegates function OnFBSuccess and OnFBFailed.

- (void)requestSucceeded 
{
    NSLog(@"requestSucceeded");
    id owner = [fbDelegate class];
    SEL selector = NSSelectorFromString(@"OnFBSuccess");
    NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
    _callback = [NSInvocation invocationWithMethodSignature:sig];
    [_callback setTarget:owner];
    [_callback setSelector:selector];
    [_callback retain];

    [_callback invokeWithTarget:fbDelegate];
}

- (void)requestFailedWithError:(NSError *)error
{
    NSLog(@"requestFailed");
    id owner = [fbDelegate class];
    SEL selector = NSSelectorFromString(@"OnFBFailed:");
    NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
    _callback = [NSInvocation invocationWithMethodSignature:sig];
    [_callback setTarget:owner];
    [_callback setSelector:selector];
    [_callback setArgument:&error atIndex:2];
    [_callback retain];

    [_callback invokeWithTarget:fbDelegate];
}

So the class taht calls method InviteFriend MUST have these functions:

-(void)OnFBSuccess
{
    CCLOG(@"successful");

    //  do stuff here  
    [login release];
}

-(void)OnFBFailed:(NSError *)error
{
    if(error == nil)
        CCLOG(@"user cancelled");
    else
        CCLOG(@"failed");

    //  do stuff here  
    [login release];
}

Recommended Reads:

Send Invitation via Facebook

API Permissions

An Example

NOTE:

1) Don't forget to setup Facebook application ID in plist.

2) Don't forget to adjust AppDelegate to handle urls.

Partial snippet taken from above link in point 2:

/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
 */
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}

Hope it helps!

EDIT

Here:

declaration for fbDelegate is:

@property (nonatomic, assign) id <FBLoginDelegate> fbDelegate;

@protocol FBLoginDelegate <NSObject>

@required

-(void) OnFBSuccess;
-(void) OnFBFailed : (NSError *)error;

@end

and this is how you can consume this code:

FBLoginHandler *login = [[FBLoginHandler alloc] initWithDelegate:self];   // here 'self' is the fbDelegate you have asked about
[login inviteFriends];