ios – 即使帖子工作正常,FBSDKSharingDelegate回调也无法正常工作

FBSDKSharingDelegate回调无法正常工作.我可以使用ios SDK发布到Facebook,但我想检测帖子是否成功,并采取其他措施通知用户.但是,回调对我不起作用.委托方法没有被调用,我不知道为什么.

使用ios8,Parse作为我的后端.在Parse中,用户链接到FB.我正在使用IOS模拟器.

我尝试过的:
我已确保授予,保存并链接到Parse用户的发布权限.我运行检查并检测到“publish_actions”.发布工作正常,因为我可以看到Facebook帐户上的帖子.这只是回调无法正常工作.我检查了我的fb设置,它看起来很好.为了最好的衡量标准,我已经包含了我的应用代表的相关代码.我用XXXX阻止了机密密钥.

码:

1st:查看用户是否登录Parse,如果没有,请发送登录并链接到Facebook帐户.完成后,我请求“发布”权限并将该附加权限链接到Parse用户.我知道这可以在我重新编译时使用b / c,它会记住“发布”权限并直接进入帖子.

@interface FacebookAPIPost () <FBSDKSharingDelegate>
@end

@implementation FacebookAPIPost
-(void)shareSegmentFacebookAPI {    //if statement below

    //1) logged in?, if not send to sign up screen
    //2) else if logged in, link account to facebook account, then send post
    //3) else send to post b/c signed up and linked already.
    PFUser *currentUser = [PFUser currentUser];
    if(!currentUser) {
        [self pushToSignIn];
    } else if(![PFFacebookUtils isLinkedWithUser:currentUser]){
        [self linkUserToFacebook:currentUser];
        NSLog(@"user account not linked to facebook");
    } else {
        [self shareSegmentWithFacebookComposer];
    }
}


-(void)linkUserToFacebook:currentUser{

    [PFFacebookUtils linkUserInBackground:currentUser withPublishPermissions:@[@"publish_actions"] block:^(BOOL succeeded, NSError *error) {
        if(error){
            NSLog(@"There was an issue linking your facebook account. Please try again.");
        }
        else {
            NSLog(@"facebook account is linked");
            //Send the facebook status update
            [self shareSegmentWithFacebookComposer];
        }
    }];
}


-(void)shareSegmentWithFacebookComposer{
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
    [self publishFBPost]; //publish
} else {
    NSLog(@"no publish permissions"); // no publish permissions so get them, then post


    [PFFacebookUtils linkUserInBackground:[PFUser currentUser]
                   withPublishPermissions:@[ @"publish_actions"]
                                    block:^(BOOL succeeded, NSError *error) {
                                        if (succeeded) {
                                            NSLog(@"User now has read and publish permissions!");
                                            [self publishFBPost];
                                        }
    }];

这是帖子的制作地点:

-(void) publishFBPost{
        FBSDKShareLinkContent *content = [FBSDKShareLinkContent new];
        content.contentURL = [NSURL URLWithString:[self.selectedSegment valueForKey:@"linkToContent"]];
        content.contentTitle = [self.selectedProgram valueForKey:@"programTitle"];
        content.contentDescription = [self.selectedSegment valueForKey:@"purposeSummary"];

    PFFile *theImage = [self.selectedSegment valueForKey:@"segmentImage"];
    NSString *urlString = theImage.url;
    NSURL *url = [NSURL URLWithString:urlString];
    content.imageURL = url;

    FBSDKShareDialog *shareDialog = [FBSDKShareDialog new];

    [shareDialog setMode:FBSDKShareDialogModeAutomatic];
//    [FBSDKShareDialog showFromViewController:self.messageTableViewController withContent:content delegate:self];
    [shareDialog setShareContent:content];
    [shareDialog setDelegate:self];
    [shareDialog setFromViewController:self.messageTableViewController];
    [shareDialog show];
}

以下代表方法无效.在帖子完成后的含义,我可以在FB帐户上看到它,但这些委托方法都不会执行.

#pragma mark - delegate methods

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
//    if ([sharer isEqual:self.shareDialog]) {
        NSLog(@"I'm going to go crazy if this doesn't work.%@",results);

        // Your delegate code
//    }
}

    - (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
    {
        NSLog(@"sharing error:%@", error);
        NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?:
        @"There was a problem sharing, please try again later.";
        NSString *title = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops!";

        [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }

    - (void)sharerDidCancel:(id<FBSDKSharing>)sharer
    {
        NSLog(@"share cancelled");
    }

控制台输出:
发布后我收到的唯一消息是几秒后出现此消息:

plugin com.apple.share.Facebook.post invalidated

请帮忙!

脚注:appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Initialize Parse.
    [Parse enableLocalDatastore];
    [Parse setApplicationId:@"XXXX"
                  clientKey:@"XXX"];
    [PFFacebookUtils initializeFacebookWithApplicationLaunchOptions:launchOptions];


    //Initialize Facebook
    [FBSDKAppEvents activateApp];
    return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}


//Method added for facebook integration
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [FBSDKAppEvents activateApp];
}

最佳答案 我想我回答这个问题太迟了,但是其他人也可能陷入这个问题,这就是为什么要分享我的知识.

As per Facebook API documentation

sharer:didCompleteWithResults: Sent to the delegate when the share completes without error or cancellation.
The results from the sharer. This may be nil or empty.

它可能是因为只有在成功共享帖子时才会调用此委托方法.如果失败,另一个委托方法sharer:didFailWithError:get.我认为Facebook API不需要添加结果参数就是这种情况.

因此,根据我的经验,如果调用sharer:didCompleteWithResults,那么这意味着成功.

点赞