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

时间:2015-08-18 18:30:44

标签: ios objective-c parse-platform facebook-sdk-4.0

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

1 个答案:

答案 0 :(得分:2)

我觉得我回答这个问题太迟了,但其他人也可能陷入这个问题,这就是为什么要分享我的知识。

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:。我认为Facebook API不需要添加 result 参数就是这种情况。

根据我的经验,如果调用此sharer:didCompleteWithResults,则表示成功。