简体   繁体   English

如何通过Facebook iOS SDK和Graph API邀请朋友加入我的应用程序

[英]How to invite friends to my application via facebook iOS SDK and Graph API

I'm writing an iPhone application. 我正在写一个iPhone应用程序。

I want to give the user the option to invite friends to start using my application via Facebook. 我想让用户选择邀请朋友开始通过Facebook使用我的应用程序。

More specifically I want to present a dialog that will let the user to select specific friends to invite. 更具体地说,我想提供一个对话框,让用户选择要邀请的特定朋友。

How can I do this? 我怎样才能做到这一点?

Thanks. 谢谢。

It's simple you can just write the below code for personalised message and also you can easily select the friend's to whom the request should be sent, it's an straight forward and powerful method. 很简单,您只需编写下面的个性化消息代码,您也可以轻松选择要向其发送请求的朋友,这是一种直接而强大的方法。

 [FBWebDialogs
 presentRequestsDialogModallyWithSession:nil
 message:NSLocalizedString(@"FBinviteMessage", nil)
 title:nil
 parameters:nil
 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {}
];

Just add this six line of code to your Button action method, then the rest will be done by the IOS & FaceBook Inbuilt framework :) 只需将这六行代码添加到Button动作方法中,其余部分将由IOS和FaceBook Inbuilt框架完成:)

You can do something like this: 你可以这样做:

Facebook* facebook = 
   [[Facebook alloc] initWithAppId:@"YOUR_FACEBOOK_APP_ID" andDelegate:self];

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       @"My Title", @"title",
                                       @"Come check out my app.",  @"message",
                                       @"FACEBOOK_USER_ID", @"to",
                                       nil]; 

[facebook dialog:@"apprequests" andParams:params andDelegate:self];

You can see the list of possible parameters at this page (scroll down): http://developers.facebook.com/docs/reference/dialogs/requests/ 您可以在此页面上看到可能的参数列表(向下滚动): http//developers.facebook.com/docs/reference/dialogs/requests/

Today with the 3.11 version of the facebook SDK you should use this in order to send an app request to a specific friend. 今天使用3.11 version的facebook SDK,你应该使用它来向特定的朋友发送应用请求。

NSString *facebookID = @"YOUR_FRIEND_FACEBOOK_ID"
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObject:facebookID forKey:@"to"];

NSString *message = @"SOME_MESSAGE";
NSString *title = @"TITLE";


FBSession *facebookSession = [PFFacebookUtils session]; //You may changed this if you are not using parse.com

[FBWebDialogs presentRequestsDialogModallyWithSession:facebookSession
                                              message:message
                                                title:title
                                           parameters:params handler:
 ^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
 {

 }];

Facebook SDK 4.0 introduced "App Invites for iOS". Facebook SDK 4.0推出了“App Appites for iOS”。 Check it out: https://developers.facebook.com/docs/app-invites/ios 请查看: https//developers.facebook.com/docs/app-invites/ios

Make sure your facebook app id is same in both developer page and info in xcode next, enable sandbox mode and must fill canvas url [under app on facebook category] in developer page. 确保您的Facebook应用程序ID在开发者页面和xcode中的信息中相同,启用沙盒模式,并且必须在开发者页面中填充画布URL [在facebook类别下的应用程序]。

NSString *facebookID = @"Your friend facebook id";;
    NSMutableDictionary* params =
    [NSMutableDictionary dictionaryWithObject:facebookID forKey:@"to"];

    NSString *message = @"SOME_MESSAGE";
    NSString *title = @"TITLE";

    [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                  message:message
                title:title
                parameters:params handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                        if (error)
                    {
                    // Case A: Error launching the dialog or sending request.
                        NSLog(@"Error sending request.");
                    }
                    else
                    {
                        if (result == FBWebDialogResultDialogNotCompleted)
                    {
                    // Case B: User clicked the "x" icon
                        NSLog(@"User canceled request.");
                    }
                    else
                    {
                        NSLog(@"Request Sent. %@", params);
                    }
        }}];

To send facebook app invites, you need to first add details of your app here.. https://developers.facebook.com/quickstarts/?platform=app-links-host 要发送Facebook应用邀请,您需要先在此处添加您的应用的详细信息.. https://developers.facebook.com/quickstarts/?platform=app-links-host

In Swift 2.2, XCode 7.3 and FBSDK 4.1 在Swift 2.2中,XCode 7.3和FBSDK 4.1

  1. import FBSDKShareKit import FBSDKCoreKit import FBSDKLoginKit import FBSDKShareKit import FBSDKCoreKit import FBSDKLoginKit

  2. Add FBSDKAppInviteDialogDelegate with your ViewController class. 使用ViewController类添加FBSDKAppInviteDialogDelegate

     func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [NSObject : AnyObject]!) { print("Initiation sent") } func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: NSError!) { print("\\(error)") } 
  3.   let content = FBSDKAppInviteContent(); content.appLinkURL = NSURL(string: "fb link that you get in above developers facebook url"); //"https:// fb.me/1775107252721102" in my case FBSDKAppInviteDialog.showFromViewController(self, withContent: content, delegate: self); 

You can either use the share dialog to share the Link to download the app to the user's wall, or write custom UI elements and use API Calls to build your own sharing module. 您可以使用共享对话框共享链接以将应用程序下载到用户的墙上,也可以编写自定义UI元素并使用API​​调用来构建您自己的共享模块。 Trying using a Facebook iOS SDK to simplify the process or you will have a lot of work to do. 尝试使用Facebook iOS SDK来简化流程,否则您将需要做很多工作。

[FBWebDialogs
 presentRequestsDialogModallyWithSession:nil
 message:@"YOUR_MESSAGE_HERE"
 title:nil
 parameters:nil
 handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
     if (error) {
         // Error launching the dialog or sending the request.
         NSLog(@"Error sending request.");
     } else {
         if (result == FBWebDialogResultDialogNotCompleted) {
             // User clicked the "x" icon
             NSLog(@"User canceled request.");
         } else {
             // Handle the send request callback
             NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
             if (![urlParams valueForKey:@"request"]) {
                 // User clicked the Cancel button
                 NSLog(@"User canceled request.");
             } else {
                 // User clicked the Send button
                 NSString *requestID = [urlParams valueForKey:@"request"];
                 NSLog(@"Request ID: %@", requestID);
             }
         }
     }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM