【问题标题】:How to open Twitter and Facebook app with Phonegap?如何使用 Phonegap 打开 Twitter 和 Facebook 应用程序?
【发布时间】:2014-05-30 12:38:48
【问题描述】:

我正在尝试使用我的 PhoneGap 应用程序链接来打开 Twitter 应用程序中的特定用户个人资料页面。我知道不是每个人都在他们的设备上安装了 Twitter 应用程序,所以如果他们没有,我想将他们发送到 Play 商店进行下载。

问题是每次我在我的 Android 设备上点击链接时都会收到错误消息:

Application Error:

net::ERR_UNKNOWN_URL_SCHEME(twitter://user?screen_name=xerxesnoble)

我的 JavaScript 如下:

//If Android

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");

if (isAndroid) {

    alert('Android!');

    twitterCheck = function() {
        alert('Android!');
        var now = new Date().valueOf();

        setTimeout(function () {
            if (new Date().valueOf() - now > 100) return;
            window.open('https://play.google.com/store/apps/details?id=com.twitter.android', '_system', 'location=no');
        }, 50);

    window.open('twitter://user?screen_name=XerxesNoble', '_system', 'location=no');
    };
};

$('.twiterNav').click(function() {
     window.open('twitter://user?screen_name=xerxesnoble', '_system', 'location=no');
});

我尝试过的事情:

  • 使用twitter:/// 代替twitter://
  • <access origin="twitter://user?screen_name=xerxesnoble" /> 添加到我的config.xml

我不确定还可以尝试什么,Facebook 也没有任何效果,但现在我一次只专注于一个问题。

【问题讨论】:

  • 将访问源更改为简单通用的:
  • 我也这样做了。

标签: javascript android facebook cordova twitter


【解决方案1】:

问题是没有安装 InAppBrowser 插件。新版本的 PhoneGap/Cordova 并没有安装所有插件——而是您选择您希望您的应用程序可以访问的内容。

在终端 cd yourApp$ cordova plugin add org.apache.cordova.inappbrowser

这样做之后,它工作得很好。

编辑

只是详细介绍一下我是如何让我的 .js 来检查是否安装了 twitter 的。

我安装了另一个插件:AppAvailability for iOS and Android

然后我将 .js 更改为如下所示:

//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');};
});
};
};

AppAvailability 插件中提供的文档也非常有用>

希望这对某人有所帮助!

【讨论】:

  • 你能在脸书应用中打开脸书页面吗?
【解决方案2】:

仅供其他可能来这里并想要更多信息的人参考:

我已经能够(到目前为止)在 iOS 和 Android 上正常运行,并使用下面的代码动态回退到浏览器。

必需的插件是cordova-plugin-inappbrowsercordova-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);
    }
}

使用很简单,只要用普通的网址调用openBrowser(url)方法即可。我发现的唯一问题是,对于一个 facebook 页面,它需要一个 ID 而不是 slug 名称

【讨论】:

  • 谢谢。很有用! Android 中 Twitter 的包名是com.twitter.android
  • 感谢您的精彩功能分享。这里有一个小错误。 “ if (!window.appAvailability) ”部分需要“返回”。没有返回码将继续下一节。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2023-03-22
  • 2017-02-21
  • 2016-04-30
  • 1970-01-01
  • 2014-04-05
  • 2013-02-24
相关资源
最近更新 更多