如何使用 FBSDKShareDialog 知道 Facebook 分享是不是完成

Posted

技术标签:

【中文标题】如何使用 FBSDKShareDialog 知道 Facebook 分享是不是完成【英文标题】:How to know if facebook sharing is completed using FBSDKShareDialog如何使用 FBSDKShareDialog 知道 Facebook 分享是否完成 【发布时间】:2016-04-14 06:37:49 【问题描述】:

我正在使用以下代码在 facebook 上分享一个链接:

FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fbauth2://"]])
        dialog.mode = FBSDKShareDialogModeNative;
    
    else 
        dialog.mode = FBSDKShareDialogModeFeedBrowser; //or FBSDKShareDialogModeAutomatic
    
    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentURL = [NSURL URLWithString:@"http://www.twipply.com"];

    dialog.shareContent = content;
    dialog.delegate = self;
    dialog.fromViewController = self;
    [dialog show];

它在以下情况下返回 didCompleteWithResults 委托: 1) 发布是从 safari 网页完成的。 (在这两种情况下都取消并完成)。 2) 从 fb 应用成功发帖。

现在如何知道链接是否实际发布。在 safari 的情况下,我们得到 postID。但在应用程序 postID 的情况下不返回。那我怎么知道它实际上已经发布了。

我使用了以下代码,但它总是进入 if 条件并且没有获取 postID 是通过应用程序发布的情况。这导致“帖子未完成,他们可能切换回应用程序”。

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults :(NSDictionary *)results 

     NSURL *fbURL = [NSURL URLWithString:@"fb://"];
    if (![[UIApplication sharedApplication] canOpenURL:fbURL])
        if (results[@"postId"]) 
            NSLog(@"Sweet, they shared, and Facebook isn't installed.");
            actionTypeSearch = kActionTypeSharing;
            sharingType = @"0";
            [self sendRequestToWeb];
        
        else 
            NSLog(@"The post didn't complete, they probably switched back to the app");
        

     else 
        NSLog(@"Sweet, they shared, and Facebook is installed.");
        actionTypeSearch = kActionTypeSharing;
        sharingType = @"0";
        [self sendRequestToWeb];
    


    NSLog(@"FB: SHARE RESULTS=%@\n",[results debugDescription]);

【问题讨论】:

【参考方案1】:
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>

 if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) 
        [self shareVideoOnFacebook];
     else 
        FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
        [loginManager logOut];  //very important line for login to work
        [loginManager logInWithPublishPermissions:@[@"publish_actions"]
                                          handler:^(FBSDKLoginManagerLoginResult result, NSError error) 
                                              if(!error) 
                                                  [self shareVideoOnFacebook];
                                              
                                              else
                                              

                                              
                                          ];
    
- (void) shareVideoOnFacebook

    __weak SharingViewController *weakSelf = self;

    NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"TestVideo" ofType:@"mp4"];

    NSData *videoData = [NSData dataWithContentsOfFile:pathFile];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3L];

    [params setObject:videoData forKey:@"video_filename.MOV"];
    [params setObject:@"Title for this post." forKey:@"title"];
    [params setObject:@"Video App" forKey:@"description"];

    [[[FBSDKGraphRequest alloc] initWithGraphpath:@"/me/videos" parameters:params HTTPMethod:@"POST"]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection connection, id result, NSError error) 
         [spinnerView endRefreshing];
         spinnerView.hidden = true;
         self.view.userInteractionEnabled = true;

         if (!error) 
             //video posted
             [[[UIAlertView alloc]initWithTitle:@"" message:@"Send Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]show];
         
         else
             NSString *strError=@"";
             NSString *strWaringMesssage;
             if ([[[[Common sharedCommon]errorList]allKeys] containsObject:strError])
             
                 NSString *strWaring=[[[Common sharedCommon]errorList] objectForKey:strError];
                 strWaringMesssage=[dictTemp objectForKey:strWaring];
             
             else
             
                 NSString *strWaring= DEFAULT_ERROR_MESSAGE;
                 strWaringMesssage=[dictTemp objectForKey:strWaring];

             
             ErrorViewController *objErrorViewController =[[ErrorViewController alloc]initWithNibName:@"ErrorViewController" bundle:nil];
             objErrorViewController.strErrorNumber = strWaringMesssage;
             [weakSelf presentViewController:objErrorViewController animated:YES completion:nil];

         
     ];

【讨论】:

在没有 publish_actions 许可的情况下,有没有办法知道数据是否已发布?因为我需要将其发送到 facebook 以进行 Publish_actions 审核。【参考方案2】:

您可以在以下FBSDKSharingDelegate 回调中检查结果字典。

- (void)sharer:(id <FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results

如果结果字典为空,则用户实际上并未在 Facebook 上发帖。如果有,它应该至少包含postID

【讨论】:

以上是关于如何使用 FBSDKShareDialog 知道 Facebook 分享是不是完成的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用原生 ios 应用程序中的 FBSDKShareDialog 的情况下在 Facebook 墙上分享照片或链接

使用 FBSDKShareDialog 发布到 facebook wall 不需要提交审核?

FBSDKShareDialog 共享对话框未打开视频共享

FBSDKShareDialog 在应该发布时取消

iOS 的 FBSDKShareDialog 回调

适用于iOS的Facebook SDK:未显示FBSDKShareDialog