【问题标题】:Open url in another application or in browser [Titanium]在另一个应用程序或浏览器中打开 url [Titanium]
【发布时间】:2015-01-18 08:42:59
【问题描述】:

我正在编写一个功能,需要在另一个应用程序[如果安装在我的手机中]或浏览器中打开一个 URL。

要在浏览器中打开网址,我可以使用Titanium.Platefor.openURL();

为了打开应用程序,我正在创建意图。

var intent = Titanium.Android.createIntent({
    packageName : appUrl,
    action : Titanium.Android.ACTION_SEND,
    data : url
});
intent.addCategory(Titanium.Android.CATEGORY_BROWSABLE);

Titanium.Android.currentActivity.startActivity(intent);

我陷入了以下问题:

  1. 如何将 url 传递给其他应用程序以打开 - 我尝试使用 url 传递 url:'http://someurl' 和数据:'http://someurl' - 但没有帮助。我得到了错误:没有找到处理 Intent 的活动

  2. 如何判断应用是否已安装?如果是 - 要求打开应用程序,如果不是 - 在浏览器中打开 url。

谁能帮忙?

提前致谢!

【问题讨论】:

  • 为什么是ios 标签?

标签: android android-intent titanium titanium-alloy


【解决方案1】:

您可以在 android 中使用带有Titanium.Platefor.openURL(); 方法的 URL 架构来识别应用程序是否安装。 (如果未安装应用程序,它将返回 false)。 对于ios,有一种方法可以识别Titanium.Platform.canOpenURL()。 您还可以将某些值传递给应用程序,例如,如果您在 ios 中打开具有源和目标 lat long 的谷歌地图应用程序,然后像这样调用

var strUrl = "http://maps.google.com/maps?saddr=" + Alloy.Globals.UserLocation.latitude + "," + Alloy.Globals.UserLocation.longitude + "&daddr=" + dLatitude + "," + dLongitude;
if (OS_IOS) {
    strUrl = "comgooglemaps://?saddr=" + Alloy.Globals.UserLocation.latitude + "," + Alloy.Globals.UserLocation.longitude + "&daddr=" + dLatitude + "," + dLongitude + "&directionsmode=driving";
    if (Titanium.Platform.canOpenURL(strUrl)) {
        Ti.Platform.openURL(strUrl);
    } else {
        strUrl = "http://maps.google.com/maps?saddr=" + Alloy.Globals.UserLocation.latitude + "," + Alloy.Globals.UserLocation.longitude + "&daddr=" + dLatitude + "," + dLongitude;
        Ti.Platform.openURL(strUrl);
    }
} else {

    var result = Ti.Platform.openURL(strUrl);
    Ti.API.info('RESULT = ' + result);
}    

再举一个例子。如果你想用给定的消息文本打开 WhatsApp 应用程序。

var whatsappUrl = encodeURI('whatsapp://send?text=' + msgBody);
    if (OS_IOS) {
        if (Ti.Platform.canOpenURL(whatsappUrl)) {
            Ti.Platform.openURL(whatsappUrl);
        } else {
            Ti.Platform.openURL("https://itunes.apple.com/ae/app/whatsapp-messenger/id310633997?mt=8");
        }
    } else {
        var isSuccess = Ti.Platform.openURL(whatsappUrl);
        if (!isSuccess) {
            Ti.Platform.openURL("https://play.google.com/store/apps/details?id=com.whatsapp&hl=en");
        }
    }

希望这对你有帮助.. :) 谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2015-05-27
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多