8

I am using the social sharing plugin for sharing messages and pictures from my mobile app to facebook and twitter,
The application will display a alert for "shared successfully" if the sharing is success and will display "sharing cancelled" if the plugin displays any error message.
For facebook it works like a charm, but in twitter there is a problem.It works fine for all scenarios except Duplicate tweets(person sharing the same tweet more than once) both the alerts are triggered as all the conditions of the plugin satisfies(internet connection, user authentication, valid message) but the twitter app in phone throws the "duplicate tweets" error late. Anyone here please help me to find that error message before triggering the alerts.

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);});
}
Vignesh
  • 99
  • 7
  • I have recently found via this blog http://cases.azoft.com/facebook-twitter-integration/ that that the error returned by twitter cannot be handled by client is there any other solution folks? – Vignesh Aug 12 '15 at 05:48

1 Answers1

1

This code is really weird. If you share a tweet with:

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

And you want to tell the user that the tweet failed, you would use the errorCallback to alert the user that the tweet had failed.

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

--additional code--

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()});
}
William Neely
  • 1,923
  • 1
  • 20
  • 23
  • just ignore the local storage part we use it for session handling and similar stuffs , the problem is "TWITTER WILL NEVER ALLOW SIMILAR TWEETS" –  Nov 05 '15 at 05:15
  • Both requests are asynchronous so when you call canShareVia you're actually not checking for an error since the error checking code will run before any error message is set. – William Neely Nov 05 '15 at 14:14
  • 1
    Yes thats right, but canShareVia actually dont check the error after entering the twitter app, and this error is due to spam prevention mechanism of twitter so it wont be captured in canShareVia –  Nov 06 '15 at 04:42
  • So why can't you just call shareViaTwitter? Why do you need canShareVia? I don't see that being one of the one's you have to check before calling (based on docs here: https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin) – William Neely Nov 06 '15 at 14:43