9

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

Serghei Pogor
  • 108
  • 1
  • 10
Ruud Visser
  • 121
  • 1
  • 4
  • are you sure that this will send a friend request? i think thats not possible [see here](http://stackoverflow.com/questions/7835311/how-send-friend-request-to-a-person-in-facebook-through-iphone-app). i use this to send a request to a friend use that app which works pretty well. – kutschenator Mar 22 '13 at 13:20
  • could it be because it doesn't have a canvas url? Noted in [this post](http://facebook.stackoverflow.com/questions/8204433/facebook-apprequests-not-working/8211249#8211249) – MonkeyBonkey Mar 23 '13 at 16:19
  • Have you found any solution? I am facing with same error all the time, even in latest SDK 3.5. Thanks – Miroslav Kovac Apr 21 '13 at 20:44
  • No still no solution.. – Ruud Visser Apr 23 '13 at 13:20
  • @MiroslavKovac, Have you checked mine's solution ? – NeverHopeless Jun 04 '13 at 12:04
  • I'm experiencing the same issue. The weird thing is that it used to work and then stopped. I haven't changed a line of code, so could it be something related to the app configuration on Facebook's side? – Valerio Santinelli Sep 29 '13 at 12:04
  • @RuudVisser, have you checked this solution ? – NeverHopeless Jan 18 '14 at 22:20

2 Answers2

16

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];
Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • It is just Awesome to show friend list. But it is giving error at getInviteFriendMessage. Saying `use of undeclared getInviteFriendMessage`. Where to declare this method? – Sushil Sharma Sep 03 '14 at 06:34
  • @iDev, this function should b declared in the same class where this invite friend list code is placed. This method should return a string containing the message to display when the invite screen appears. – NeverHopeless Sep 03 '14 at 06:39
  • Thanks for your reply. I have more queries.Can you help me out? Can i add message as a string if i don't want to create `getInviteFriendMessage` method ? and what is `_callBack` ? Where did you declare this `_callBack` ? – Sushil Sharma Sep 03 '14 at 07:35
  • You can change function call to `message:@"your message here"` and define this in .h file `NSInvocation *_callback` – NeverHopeless Sep 03 '14 at 07:40
  • Oops, it is giving exception `WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: +[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil`. and leaving webDialog blank. I think i did something wrong. – Sushil Sharma Sep 03 '14 at 07:46
  • I think you should ask a separate question in reference to this question. So you may get assisted properly. Since there are couple of clarifications required like SDK version, did you tried with `delegate` instead of `NSInvocation` etc. – NeverHopeless Sep 03 '14 at 08:00
  • 2
    @iDev, here i have added a working sample code, which might help you: http://journeytoios.wordpress.com/2014/09/03/facebook-invite-friend-implementation/ – NeverHopeless Sep 03 '14 at 21:06
  • Thanks for the code But not working yet.Not sending any message to Facebook friends. I have added a new question . Please Check my code there [Here is the link](http://stackoverflow.com/questions/25640920/sending-invitation-to-facebook-friends-with-facebook-sdk-from-ios-app) – Sushil Sharma Sep 04 '14 at 04:41
  • Thanks for the code ! it works for me with FBSDK v3.19. It sends requests to Facebook friends. – youssman Oct 08 '14 at 09:43
  • What about custom dialog? is it possible? – ozba Oct 10 '14 at 23:54
  • @ozba, I think you can retrieve friends and show in custom dialog but then you can't send the request, `FBWebDialogs` manages both to get the friends and sending a request. – NeverHopeless Oct 11 '14 at 08:57
  • @NeverHopeless Can a Requests dialog (FBWebDialogs) be even used in an iOS game that has no presence on Facebook Canvas? Because in the facebook faq it says that an app that does not have a presence on Facebook Canvas may use the Message Dialog on iOS, not the requests dialog. I elaborated more on the subject on the question I've posted here:http://stackoverflow.com/questions/26309819/building-a-custom-facebook-invite-dialog-via-facebook-ios-sdk-3 – ozba Oct 11 '14 at 10:36
  • @ozba, I found this: http://stackoverflow.com/questions/11521580/how-to-send-add-friend-request-to-facebook-user-from-ios-application. Not sure if that works correctly but I guess facebook will not encourage it see facebook policy->point 4.2 as mentioned in the link under the answer. – NeverHopeless Oct 11 '14 at 21:47
0

I think your application is not enable for Android and for web . And you are trying to get notification on web or on Android device.

Points : For getting notification on Android or on web you have to enable your app for Android and web too .

To Enable Android and Web on your App : GoTo your App > Setting > Click on + Add platform add enter necessary information and Save .

Lets Enjoy Notification . :-)

Alok
  • 24,880
  • 6
  • 40
  • 67