How to send app invitation to facebook friends from ios app

698 Views Asked by At

I have complete code for send invitation when i click on button a pop up menu is appear,in the pop up menu say...

ERROR.

Game Requests are available to games.

and my code for invite friends are here:

 NSDictionary *parameters = @{@"to":@""};

[FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                              message:@"message aaya kya"                                                    title:@"app request"
                                           parameters:parameters
                                              handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
 {
     if(error)
     {
         NSLog(@"Some errorr: %@", [error description]);
         UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Invitiation Sending Failed" message:@"Unable to send inviation at this Moment, please make sure your are connected with internet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
         [alrt show];
        // [alrt release];
     }
     else
     {
         if (![resultURL query])
         {
             return;
         }
         
         NSDictionary *params = [self parseURLParams:[resultURL query]];
         NSMutableArray *recipientIDs = [[NSMutableArray alloc] init];
         for (NSString *paramKey in params)
         {
             if ([paramKey hasPrefix:@"to["])
             {
                 [recipientIDs addObject:[params objectForKey:paramKey]];
             }
         }
         if ([params objectForKey:@"request"])
         {
             NSLog(@"Request ID: %@", [params objectForKey:@"request"]);
         }
         if ([recipientIDs count] > 0)
         {
             //[self showMessage:@"Sent request successfully."];
             //NSLog(@"Recipient ID(s): %@", recipientIDs);
             UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Invitation(s) sent successfuly!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
             [alrt show];
             //[alrt release];
         }
         
     }
 }
                                          friendCache:nil];


}

so where I am wrong? Please help me. Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

Ok,I understand if you want to send app request to your friends than you should use FBSDKAppInviteContent.

Here is the code:

FBSDKAppInviteContent *content =[[FBSDKAppInviteContent alloc] init];
content.appLinkURL = [NSURL URLWithString:@"Your_App_Id"];
content.previewImageURL = [NSURL URLWithString:@"Your_app_previewimage"];
[FBSDKAppInviteDialog showWithContent:content
                             delegate:self];

Here for Your_App_Id please refer to this link.

And it's delegate methods:

- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didCompleteWithResults:(NSDictionary *)results{
if (results) {

   }
}
- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didFailWithError:(NSError *)error{
if (error) {

    NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
    @"There was a problem sending the invite, please try again later.";
    NSString *title = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops!";

    [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
   }
}
5
On

Why don't you try FBSDKGameRequestContent(It will require Facebook SDK 4.0)?

Note: This will work only if your app category is Game in Facebook developer page.

Here is the code:

FBSDKGameRequestContent *gameRequestContent = [[FBSDKGameRequestContent alloc] init];
     // Look at FBSDKGameRequestContent for futher optional properties
     FBSDKGameRequestDialog *dialog = [[FBSDKGameRequestDialog alloc]init];
     dialog.delegate = self;
     dialog.content = gameRequestContent;

     gameRequestContent.message = @"Become a Ninja!!!";
     gameRequestContent.title = @"NinjaPan";
     dialog.delegate = self;
     dialog.content = gameRequestContent;
     [dialog show];

And it's delegate methods:

- (void)gameRequestDialog:(FBSDKGameRequestDialog *)gameRequestDialog didCompleteWithResults:(NSDictionary *)results{
if (results) {

      }
}

- (void)gameRequestDialog:(FBSDKGameRequestDialog *)gameRequestDialog didFailWithError:(NSError *)error{
if (error) {
    NSLog(@"%@",error.localizedDescription);
   }
}

- (void)gameRequestDialogDidCancel:(FBSDKGameRequestDialog *)gameRequestDialog{
NSLog(@"Cancelled by user");
}