如何知道Facebook共享是否使用FBSDKShareDialog完成

时间:2016-04-14 06:37:50

标签: ios xcode facebook

我使用以下代码在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 webPage完成的。 (在两种情况下取​​消和完成)。 2)从fb app成功发布。

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

我使用了以下代码,但它总是进入if条件而不是获得postID是通过app发布的情况。这导致"帖子没有完成,他们可能会切换回应用程序"。

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

2 个答案:

答案 0 :(得分:0)

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

         }
     }];
}

答案 1 :(得分:0)

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

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

如果结果字典为空,则用户实际上并未发布到Facebook。如果他们这样做,则至少应包含postID