简体   繁体   English

iOS中的Facebook Share Dialog

[英]Facebook Share Dialog in iOS

I am trying to implement the native Share dialog from Facebook in a sample application. 我试图在示例应用程序中从Facebook实现本机共享对话框

Seem to have some problem in doing so. 这样做似乎有些问题。

Things I have done so far: 到目前为止我做的事情:

  1. Included the latest Facebook SDK 包括最新的Facebook SDK
  2. Included the AdSupport, Social, Account, Security and libsqlite3.dylib. 包括AdSupport,Social,Account,Security和libsqlite3.dylib。
  3. Added the sample code from Facebook. 从Facebook添加了示例代码。
  4. Added -lsqlite3.0 -ObjC to Other Linker Flags as well 将-lsqlite3.0 -ObjC添加到其他链接器标志中
  5. Added the app id to the plist 将应用程序ID添加到plist
  6. Added the FBUserSettingsViewResources bundle and FacebookSDKResources.bundle to the project 将FBUserSettingsViewResources包和FacebookSDKResources.bundle添加到项目中

But I can't seem to share the URL. 但我似乎无法分享网址。 The share dialog doesn't even appear. 共享对话框甚至没有出现。

My code looks like this: 我的代码看起来像这样:

Viewcontroller.h Viewcontroller.h

#import <UIKit/UIKit.h>

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

ViewController.m 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

Not able to share the URL. 无法共享该网址。 Need some guidance on this. 需要一些指导。

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

Do you have Facebook App installed on your device? 您的设备上安装了Facebook App吗? The Share dialog only works if you have the Facebook app installed. 只有安装了Facebook应用程序,“共享”对话框才有效。

From the Share dialog documentation : 从“共享”对话框文档中:

Tip 1: What if people don't have the Facebook app installed? 提示1:如果人们没有安装Facebook应用程序怎么办?

The Share dialog can't be displayed if people don't have the Facebook app installed. 如果人们没有安装Facebook应用程序,则无法显示“共享”对话框。 Apps can detect this by calling [FBDialogs canPresentShareDialogWithParams:nil]; 应用可以通过调用[FBDialogs canPresentShareDialogWithParams:nil];来检测到这一点[FBDialogs canPresentShareDialogWithParams:nil]; and may disable or hide a sharing button or fall back to the Feed dialog to share on the web. 并可以禁用或隐藏共享按钮或回退到Feed对话框以在Web上共享。 See the HelloFacebookSample included with the iOS SDK for an example. 有关示例,请参阅iOS SDK附带的HelloFacebookSample。

From what I saw, iOS 6 will return NO for + canPresentShareDialogWithParams: . 从我看到的情况来看,iOS 6将为+ canPresentShareDialogWithParams:返回NO + canPresentShareDialogWithParams: . It only responds to + canPresentOSIntegratedShareDialogWithSession: . 它只响应+ canPresentOSIntegratedShareDialogWithSession: . But I could be wrong here. 但我在这里错了。

Anyways, this is how I do it - 1. For sharing links : 无论如何,这是我的方式 - 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.For OpenGraph calls : 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");
                                                 }
                                             }];

Check out other ways to share on iOS here 这里查看在iOS上分享的其他方式

Hope this helps. 希望这可以帮助。

This works for me: 这对我有用:

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

}

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

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