iOS中的Facebook分享对话框

3

我正在尝试在示例应用程序中实现Facebook的本地分享对话框

似乎在这样做时出现了一些问题。

到目前为止,我已经完成了以下工作:

  1. 包括最新版本的Facebook SDK
  2. 包括AdSupport、Social、Account、Security和libsqlite3.dylib。
  3. 添加了来自Facebook的示例代码。
  4. 将-lsqlite3.0 -ObjC也添加到其他链接器标志中。
  5. 将应用程序ID添加到plist中。
  6. 向项目添加了FBUserSettingsViewResources bundle和FacebookSDKResources.bundle。

但是我似乎无法共享URL。分享对话框甚至都没有出现。

我的代码看起来像这样:

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIButton *buttonShare;
}
- (IBAction)shareButtonClicked:(id)sender;
@end

ViewController.m

#import "ViewController.h"
#import <FacebookSDK/FacebookSDK.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)shareButtonClicked:(id)sender
{
    FBShareDialogParams *params = [[FBShareDialogParams alloc] init];
    params.link = [NSURL URLWithString:@"https://developers.facebook.com/ios"];
    params.picture = [NSURL URLWithString:@"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"];
    params.name = @"Facebook SDK for iOS";
    params.caption = @"Build great apps";
    [FBDialogs presentShareDialogWithParams:params clientState:nil handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
        if(error) {
            NSLog(@"Error: %@", error.description);
        } else {
            NSLog(@"Success!");
        }
    }];
}
@end

无法分享URL。需要一些指导。


你使用的 Facebook 应用程序版本是什么? - Ming Li
@MingLi 你如何进行检查? - lakshmen
在您的手机设置中,向下滚动到Facebook,它应该在那里的详细信息中显示版本(请注意,由于iOS6集成和Facebook应用程序,您可能会有2个Facebook按钮)。 - Ming Li
4个回答

1
- (IBAction)shareButtonClicked:(id)sender
{
    // if the session is closed, then we open it here, and establish a handler for state changes

    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
     {
         if (error)
         {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alertView show];
         }
         else if(session.isOpen)
         {
             NSString *str_img = [NSString stringWithFormat:@"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"];

             NSDictionary *params = @{
                                      @"name" :[NSString stringWithFormat:@"Facebook SDK for iOS"],
                                      @"caption" : @"Build great Apps",
                                      @"description" :@"Welcome to iOS world",
                                      @"picture" : str_img,
                                      @"link" : @"",
                                      };

             // Invoke the dialog
             [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                                    parameters:params
                                                       handler:
              ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                  if (error) {
                      //NSLog(@"Error publishing story.");
                      [self.indicator stopAnimating];
                  } else {
                      if (result == FBWebDialogResultDialogNotCompleted) {
                          //NSLog(@"User canceled story publishing.");
                          [self.indicator stopAnimating];
                      } else {
                          //NSLog(@"Story published.");
                          [self.indicator stopAnimating];
                      }
                  }}];
         }

     }];

    return;
}

0
- (IBAction)btn_facebook:(id)sender {
    [self performSelector:@selector(fb_func) withObject:nil afterDelay:0.0];
}

-(void)fb_func
{
    // if the session is closed, then we open it here, and establish a handler for state changes

    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
     {
         if (error)
         {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alertView show];
         }
         else if(session.isOpen)
         {
             NSString *str_link = @"";
             NSLog(@"%@",str_link);

             NSDictionary *params = @{
                                      @"name" :@"name",
                                      @"caption" : @"Description",
                                      @"description" :@"test",
                                      @"picture" : PostimageToPintresrAndFacebook,
                                      @"link" : @"url",
                                      };

             // Invoke the dialog
             [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                                    parameters:params
                                                       handler:
              ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                  if (error) {
                      NSLog(@"Error publishing story.");
                  } else {
                      if (result == FBWebDialogResultDialogNotCompleted) {
                          NSLog(@"User canceled story publishing.");
                      } else {
                          NSLog(@"Story published.");
                      }
                  }}];
         }
     }];
    return;

}

0
这对我有效:
NSString *url = @"myUrl";
FBLinkShareParams* params = [[FBLinkShareParams alloc]init];
params.link = [NSURL URLWithString:url];

if([FBDialogs canPresentShareDialogWithParams:params]){
        [FBDialogs presentShareDialogWithLink:  [NSURL URLWithString:url]
        name:   @"Name"
        caption:    @"Caption"
        description:    @"Description"
        picture:    nil
        clientState:    nil
        handler:    nil];
    }

    else{
          [...]
    }

0

您的设备上是否安装了Facebook应用程序?只有在安装了Facebook应用程序的情况下,分享对话框才能正常工作。

来自分享对话框文档:

提示1:如果用户没有安装Facebook应用程序怎么办?

如果用户没有安装Facebook应用程序,则无法显示分享对话框。应用程序可以通过调用[FBDialogs canPresentShareDialogWithParams:nil];来检测此情况,并可能禁用或隐藏共享按钮,或者回退到Feed对话框以在Web上进行分享。请参见iOS SDK中包含的HelloFacebookSample示例。

根据我所看到的,iOS 6将返回NO,表示+ canPresentShareDialogWithParams:无法响应。它只响应于+ canPresentOSIntegratedShareDialogWithSession:。但是我可能在这里错了。

无论如何,这是我的做法 - 1. 对于分享链接:

if ([FBDialogs canPresentShareDialogWithParams:nil]) {

    NSURL* url = [NSURL URLWithString:link.url];
    [FBDialogs presentShareDialogWithLink:url
                              handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                 if(error) {
                       NSLog(@"Error: %@", error.description);
                 } else {
                       NSLog(@"Success");
                 }
     }];
}

2. 对于 OpenGraph 调用:

id<FBGraphObject> pictureObject =
    [FBGraphObject openGraphObjectForPostWithType:@"your_namespace:picture"
                                            title:image.title
                                            image:image.thumbnailUrl
                                              url:image.url
                                      description:@""];

    id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
    [action setObject:pictureObject forKey:@"picture"];

    [FBDialogs presentShareDialogWithOpenGraphAction:action
                                          actionType:@"your_namespace:action_name"
                                 previewPropertyName:@"picture"
                                             handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                 if(error) {
                                                     NSLog(@"Error: %@", error.description);
                                                 } else {
                                                     NSLog(@"Success");
                                                 }
                                             }];

查看其他在iOS上分享的方法这里

希望这可以帮到你。


你能否使用上述代码分享并使用sharedialog?顺便问一下,你是使用FBGraphObject还是OpenGraph调用? - lakshmen
是的,我可以毫不费力地分享。可以使用Open Graph调用来创建故事。例如,您可以发布类似于“用户在your_app上创建了一个食谱”的内容。请Google一下。 - Rohith Nandakumar
用户完成操作后,presentShareDialogWithLink处理程序会被调用吗? - Zorayr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接