Phonegap / cordova社交分享插件,重复推文问题

时间:2015-08-11 07:01:14

标签: javascript twitter phonegap-plugins cordova-plugins

我使用社交分享插件从我的移动应用程序分享消息和图片到facebook和twitter,
应用程序将显示"成功共享的警报"如果共享成功并将显示"共享已取消"如果插件显示任何错误消息。
对于Facebook来说,它就像一个魅力,但在twitter中有一个问题。除了重复推文(人多次共享同一条推文)之外,它适用于所有场景。当插件的所有条件满足时,两个警报都会被触发(互联网)连接,用户身份验证,有效消息)但电话中的推特应用程序抛出了重复的推文"错误很晚。这里的任何人请帮助我在触发警报之前找到该错误消息。

function shareViaTwitter(id, message, image, url) {
    canShareViaTwitter();
    message = message.replace(/<br *\/?>/gi,'\n');

var successmessage  = window.localStorage.getItem('canshareTwitsuccess');
var errormessage    = window.localStorage.getItem('canshareTwiterror');

if(errormessage != '' && errormessage != 'null' && errormessage != null)
{
    navigator.app.loadUrl('https://play.google.com/store/apps/details?id=com.twitter.android&hl=en', { openExternal:true });
    reloadPage();
}
else
{   
    if(navigator.onLine) {
        var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());

        if(isAndroid) {
            message = (message) ? message : null;
            image = (image) ? image : null;
            url = (url) ? url : null;

            window.plugins.socialsharing.shareViaTwitter(message, image, url, function(msg){}, function(msg){});
            $("#sharingText").html("Shared Successfully");
            $('#sharing-sucess-trigger').trigger('click');
        }
    } else {
        $("#sharingText").html("Shared Failed! \nNo Network Connection");
        $('#sharing-sucess-trigger').trigger('click');
    }
    reloadPage();
}
}

function canShareViaFacebook()
{
    //alert("canshare")
    window.plugins.socialsharing.canShareVia('com.facebook.katana', 'msg', null, null, null, function(fbsuccess){window.localStorage.setItem("canshareFbsuccess", fbsuccess);}, function(fberror){window.localStorage.setItem("canshareFberror", fberror);});
}

function canShareViaTwitter()
{
    //alert("canshare")
    window.plugins.socialsharing.canShareVia('twitter', 'msg', null, null, null, function(twitsuccess){window.localStorage.setItem("canshareTwitsuccess", twitsuccess);}, function(twiterror){window.localStorage.setItem("canshareTwiterror", twiterror);});
}

1 个答案:

答案 0 :(得分:1)

这段代码真的很奇怪。如果您与以下内容共享推文:

socialsharing.shareViaTwitter(message, file, url, successCallback, errorCallback)

如果您想告诉用户推文失败,您可以使用errorCallback提醒用户推文失败。

function errorCallback(msg){
    alert(msg);
}

- 附加代码 -

function shareViaTwitter(id, message, image, url) {
    message = message.replace(/<br *\/?>/gi,'\n');
    canShareViaTwitter(function(){
        if(navigator.onLine) {
            var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());

            if(isAndroid) {
                message = (message) ? message : null;
                image = (image) ? image : null;
                url = (url) ? url : null;

                window.plugins.socialsharing.shareViaTwitter(message, image, url, function(msg){}, function(msg){});
                $("#sharingText").html("Shared Successfully");
                $('#sharing-sucess-trigger').trigger('click');
            }
        }
        else {
            $("#sharingText").html("Shared Failed! \nNo Network Connection");
            $('#sharing-sucess-trigger').trigger('click');
        }
        //why?
        reloadPage();
    },
    function(){
        navigator.app.loadUrl('https://play.google.com/store/apps/details?id=com.twitter.android&hl=en', { openExternal:true });
        reloadPage();
    });
}
function canShareViaTwitter(successCallback, errorCallback)
{
    //alert("canshare")
    window.plugins.socialsharing.canShareVia('twitter', 'msg', null, null, null, function(twitsuccess){window.localStorage.setItem("canshareTwitsuccess", twitsuccess);successCallback();}, function(twiterror){window.localStorage.setItem("canshareTwiterror", twiterror);errorCallback()});
}