How to open Twitter and Facebook app with Phonegap?

后端 未结 2 520
小鲜肉
小鲜肉 2020-12-29 14:20

I am trying to have my PhoneGap application link to open a specific users profile page in the Twitter app. I know not everyone has the Twitter app installed on their device

相关标签:
2条回答
  • 2020-12-29 15:07

    Just a reference for others who might come here and want a bit more:

    I've been able to get this working well (so far) on both iOS and Android, and dynamically falling back to the browser using the code below.

    Required plugins are cordova-plugin-inappbrowser and cordova-plugin-appavailability

    function openBrowser (url) {
        if (!url) {
            return;
        }
    
        // turn my url into a scheme/intent url
        getAvailabilityScheme(url, function (url) {
            window.open(url, '_system');
        });
    }
    
    function getAvailabilityScheme (url, callback) {
        var schemeOrPackage;
        var schemeUrl;
    
        // check for appavaialbility plugin
        if (!window.appAvailability) {
            callback(url);
        }
    
        // facebook
        if (url.indexOf('facebook.com/') !== -1) {
            schemeOrPackage = isAndroid ? 'com.facebook.katana' : 'fb://'
            schemeUrl = 'fb://profile/' + url.split('facebook.com/')[1];
        }
    
        // twitter
        if (url.indexOf('twitter.com/') !== -1) {
            schemeOrPackage = isAndroid ? null : 'twitter://'
            schemeUrl = 'twitter://user?screen_name=' + url.split('twitter.com/')[1];
        }
    
        if (schemeOrPackage && schemeUrl) {
            window.appAvailability.check(schemeOrPackage, function () {
                callback(schemeUrl);
            }, function () {
                callback(url);
            });
        } else {
            callback(url);
        }
    }
    

    Using it is easy, just call the openBrowser(url) method with a normal website url. The only issue i found was that for a facebook page, it needs to an ID not the slug name

    0 讨论(0)
  • 2020-12-29 15:11

    The Problem is that the InAppBrowser plugin was not installed. New versions of PhoneGap/Cordova do not come with all plugins installed- instead you choose what you want your App to have access to.

    In terminal cd yourApp and $ cordova plugin add org.apache.cordova.inappbrowser

    After doing that, it worked perfectly.

    EDIT

    Just to branch out a little bit more on how I got my .js to check if twitter was installed or not.

    I installed another plugin : AppAvailability for iOS and Android

    Then I altered my .js to look like this:

    //Twitter checker
    
    // If Mac//
    
    var twitterCheck = function(){
    
    appAvailability.check('twitter://', function(availability) {
        // availability is either true or false
        if(availability) { window.open('twitter://user?screen_name=xerxesnoble', '_system', 'location=no');}
        else{window.open('https://itunes.apple.com/ca/app/twitter/id333903271?mt=8', '_system', 'location=no'); };
    });
    };
    
    //If Android
    
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    
    if(isAndroid) {
    
    twitterCheck = function(){    
    
    appAvailability.check('com.twitter.android', function(availability) {
        // availability is either true or false
        if(availability) {window.open('twitter://user?screen_name=xerxesnoble', '_system', 'location=no');}
        else{window.open('https://play.google.com/store/apps/details?id=com.twitter.android', '_system', 'location=no');};
    });
    };
    };
    

    The documentation provided in the AppAvailability plugin was very helpful as well>

    Hope that this helps someone!

    0 讨论(0)
提交回复
热议问题