Porwal
Porwal

Reputation: 330

Open url in another application or in browser [Titanium]

I am writing a functionality that needs to open a URL either in the another app [if installed in my phone] or else, in the browser.

To open the URL in browser, I can use Titanium.Platefor.openURL();

To open the app I am creating the intent.

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);

I have stuck in below things:

  1. How to pass the url to other app to open - I tried passing url using url : 'http://someurl' and data: 'http://someurl' - but didn't help. I got the error: No Activity found to handle Intent

  2. How to find out whether the app is install or not? If yes - ask for the application to open, if no - open the url in browser.

Can anyone help?

Thanks in advance!

Upvotes: 0

Views: 1819

Answers (1)

Nilesh Patel
Nilesh Patel

Reputation: 147

You can identify app is install or not using URL schema with Titanium.Platefor.openURL(); method in android. (if app is not installed it will return false). and for ios there is one method for identify Titanium.Platform.canOpenURL(). and also you can passed something value to application for example if you open google map application with source and destination lat long in ios then call like this

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

one more example.. if you want opening whatsApp application with given message text.

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");
        }
    }

Hop this is helps you.. :) Thanks

Upvotes: 2

Related Questions