javascript 如何使用 Phonegap 打开 Twitter 和 Facebook 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23095906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Twitter and Facebook app with Phonegap?
提问by xno
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 so I wanted to send them to the Play Store to download it if they didn't.
我试图让我的 PhoneGap 应用程序链接打开 Twitter 应用程序中的特定用户个人资料页面。我知道不是每个人的设备上都安装了 Twitter 应用程序,所以我想将他们发送到 Play 商店下载,如果他们没有的话。
Problem is that every time I tap the link on my Android device I receive an error:
问题是,每次我点击 Android 设备上的链接时,都会收到错误消息:
Application Error:
net::ERR_UNKNOWN_URL_SCHEME(twitter://user?screen_name=xerxesnoble)
My JavaScript is as follows:
我的 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');
});
Things that I've tried:
我尝试过的事情:
- Using
twitter:///
instead oftwitter://
- Adding
<access origin="twitter://user?screen_name=xerxesnoble" />
to myconfig.xml
- 使用
twitter:///
代替twitter://
- 添加
<access origin="twitter://user?screen_name=xerxesnoble" />
到我的config.xml
I'm not sure what else to try, nothing is working for Facebook either but right now I'm focusing on one issue at a time.
我不知道还能尝试什么,Facebook 也没有任何效果,但现在我一次只关注一个问题。
回答by xno
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.
问题是未安装 InAppBrowser 插件。新版本的 PhoneGap/Cordova 并未安装所有插件 - 而是您选择您希望应用程序访问的内容。
In terminal cd yourApp
and $ cordova plugin add org.apache.cordova.inappbrowser
在终端cd yourApp
和$ 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.
只是为了更多地了解我如何让我的 .js 来检查是否安装了 twitter。
I installed another plugin : AppAvailability for iOS and Android
我安装了另一个插件:适用于 iOS 和 Android 的 AppAvailability
Then I altered my .js to look like this:
然后我改变了我的 .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');};
});
};
};
The documentation provided in the AppAvailability plugin was very helpful as well>
AppAvailability 插件中提供的文档也非常有帮助>
Hope that this helps someone!
希望这对某人有所帮助!
回答by Deminetix
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.
我已经能够在 iOS 和 Android 上运行良好(到目前为止),并使用下面的代码动态回退到浏览器。
Required plugins are cordova-plugin-inappbrowser
and cordova-plugin-appavailability
所需的插件是cordova-plugin-inappbrowser
和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
使用它很容易,只需openBrowser(url)
使用普通网站 url调用该方法即可。我发现的唯一问题是,对于 facebook 页面,它需要一个 ID 而不是 slug 名称