如何检测用户取消共享时,使用fb.ui

How to detect user cancelled share when using fb.ui

本文关键字:共享 使用 ui fb 取消 用户 何检测 检测      更新时间:2023-09-26

我使用这里提供的文档和下面的代码。正确弹出共享对话框。问题是,我无法区分用户在对话框中采取的"取消"answers"发布"动作。我想这应该是回应的一部分。

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/',
}, function(response){
    if (response && !response.error_code) {
        console.log(response);
    } else {
        alert('Error while posting.');
    }
});

编辑:控制台的输出不是提供任何方法知道

Cancel - Object {e2e: "{"submit_0":1401181811121}"} 
Post - Object {e2e: "{"submit_0":1401181815112}"} 

我对此进行了测试,显然在response对象中有一些信息可以用来确定对话框是否被取消。

代码
FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});

取消时输出:

{error_code: 4201, error_message: "User+canceled+the+Dialog+flow", e2e: "{"submit_0":1401188820613}"} 

我想你可以像这样检查cancellaction:

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/'
}, function(response){
    if (response && !response.error_code) {
        console.log("OK: "+JSON.stringify(response));
    } else if (response && response.error_code === 4201) { //Cancelled
        console.log("User cancelled: "+decodeURIComponent(response.error_message));
    } else {
        console.log("Not OK: "+JSON.stringify(response));
    }
});

不幸的是,FB.Events.subscribe()没有提供一个事件来取消这个对话框:https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.0

这是有意劝阻开发人员不要将发布作为一种控制机制。这应该由用户自己决定是否发布,而不应该是应用程序的要求。

使用"feed"方法代替"share"方法,这样就不需要应用程序的许可来获取响应。

FB.ui({
    method: 'feed',
    caption: 'My Caption',
    link: 'http://www.google.com/'
}, function(response) {
    if (response && response.post_id) {
        alert('Thank you for sharing!');                
    } else {
        alert('You have cancelled the share.');
    }
});

我也有响应函数的问题,我目前正在编码并试图使用fb。UI

    return FB.ui({
      method: 'share',
      href: this.shareUrl,
      hashtag: "myHashTag",
      quote: "myQuote"
    }, function(res) {
      console.log("res = ", res);
      console.log("res? = ", res != null);
      return App.vent.trigger("FBShareView:cancelled");
    });

我发现,在一个成功的共享,res是一个空数组和res != null是真

我发现对于取消场景,res是未定义的。

我希望看到res作为一个带有error_message的对象,如下所示:https://developers.facebook.com/docs/sharing/reference/share-dialog

你能告诉我哪里出了问题吗?