如何从 ios 应用向 Facebook 好友发送应用邀请

How to send app invitation to facebook friends from ios app

我有完整的发送邀请代码,当我点击按钮时会出现一个弹出菜单,在弹出菜单中说...

ERROR.

Game Requests are available to games.

和我的邀请朋友代码在这里:

 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];


}

所以我哪里错了? 请帮我。 谢谢

为什么不试试 FBSDKGameRequestContent(它需要 Facebook SDK 4.0)?

注意:仅当您的应用类别是 Facebook 开发者页面中的游戏时,这才有效。

代码如下:

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];

它是委托方法:

- (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");
}

好的,我知道如果你想向你的朋友发送应用程序请求,你应该使用 FBSDKAppInviteContent。

代码如下:

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

这里Your_App_Id请参考这个link.

它是委托方法:

- (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];
   }
}