【问题标题】:How to detect user cancelled share when using fb.ui使用 fb.ui 时如何检测用户取消共享
【发布时间】:2014-05-27 08:30:19
【问题描述】:

我正在使用通过here 提供的文档以及以下代码。共享对话框正确出现。问题是我无法区分用户在对话框中执行的“取消”和“发布”操作。我想这将是响应的一部分。

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}"} 

【问题讨论】:

    标签: javascript facebook


    【解决方案1】:

    我对此进行了测试,显然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}"} 
    

    所以,我想您可以像这样检查取消操作:

    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

    【讨论】:

    • 这就是我的想法,但在这两种情况下,响应都不为空。我已经编辑了答案以将响应对象显示为控制台的输出
    • 嗯,取消的“版本”的输出看起来很奇怪。人们可能会认为无论如何都会创建一个对象。能否请您添加您创建的代码以输出到控制台?
    • 我在上面的sn-p代码中有一条语句“console.log”。
    • 我读到了,但只有在response 中没有error_code 时才会触发console.log,所以这不应该产生你的输出。
    • 我无法让它工作。用户取消时没有错误代码。为此创建了一个 plnkr - embed.plnkr.co/4sd0kRv3anqZt5YvPHGY/preview
    【解决方案2】:

    这是故意阻止开发人员使用发布作为门控机制。发不发应该由人来决定,这不应该是应用程序的要求。

    【讨论】:

    • 我也怀疑可能是这种情况。我还没能得到托比的答案。
    【解决方案3】:

    使用“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.');
        }
    });
    

    【讨论】:

    • 如果 (response && response.post_id) 在单击取消按钮时在 chrome 中不起作用。
    • 参数 post_id 在 6 个月的弃用窗口内,无需获得 publish_actions 权限即可使用。从 2016 年 10 月 12 日开始
    【解决方案4】:

    我的响应功能也有问题,我目前正在编码并尝试使用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 是 true

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

    我希望将 res 视为带有 error_message 的对象,如下所述:https://developers.facebook.com/docs/sharing/reference/share-dialog

    你能告诉我可能出了什么问题吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      相关资源
      最近更新 更多