web intent working sample code

  • 1
  • Question
  • Updated 5 years ago
  • Answered
hello as a newbie in phonegap app building, i am trying to make use of the webintent plugin to open an external app from my app, can i get a sample code example on how this works, thanks
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
There are multiple plugins named WebIntent. I'd go with the net.tunts one since it is used by the most number of apps.

In your config.xml you'd need to add the following:

<gap:plugin name="net.tunts.webintent" />

In your index.html you could add:

<script>

function testWebIntent() {
address = "1600+Amphitheatre+Parkway%2C+CA" ;
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: 'geo:0,0?q=' + address},
function() {alert('success')},
function() {alert('Failed to open URL via Android Intent')}
);
}
</script>
<button onclick='testWebIntent()'>Test WebIntent</button>
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
ok gud and what if i want to open an external app using this plugin for phonegap 3 pgb build how do i go about this.

i tried this

<input type="button" onclick="javascript:whatsapp();" value="whatsapp">

</div>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="webintent.js"></script>

<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();

function whatsapp() {
alert('open');
window.plugins.webintent.startActivity({
action: 'com.whatsapp'},
function() {alert('Started!?');},
function() {alert('Failed to open URL via Android Intent');}
);
}
</script>


and i keep getting "Failed to open URL via Android Intent"

how do i solve this.
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
According to the FAQ for whatsapp at http://www.whatsapp.com/faq/end/andro... you need to call setPackage on the intent. However, this plugin doesn't allow you to do that. The closest I got was:

window.plugins.webintent.startActivity({

action: window.plugins.webintent.ACTION_SEND,
extras: {
'android.intent.extra.TEXT': 'This is my text to send.'
},
type: 'text/plain'
},
function() {alert('success')},
function(errorMsg) {alert('Failed to startActivity errorMsg=' + errorMsg)}
);


Maybe one of the other WebIntent plugins would work. Or maybe we can get the author of this plugin to update his.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
if u could give me an alternative plugin and sample code that works and builds with the pgb, would really appreciate
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
I did get the WhatsApp app to launch using:

window.plugins.webintent.startActivity({

action: window.plugins.webintent.ACTION_VIEW,
url: 'whatsapp://send?text=Hello%20World!'
},
function() {alert('success')},
function(errorMsg) {alert('Failed to startActivity errorMsg=' + errorMsg)}
);
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
ok good, what if i wanted to launch other external apps, could i just replace "whatsapp" with another app like badoo ?
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
You'd have to research to see if the other apps have set up a custom url scheme or not.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
please how do i go abt this ? google or where
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Probably look at the particular company's website to see if they have published anything. WhatsApp had a FAQ that gave the necessary details. If you find a mobile webpage that launches an app via a link then you could use the same URL from your app.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
all right thanks
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
hello when i try the snippet u gave me i get this error "Failed to startActivity" what could i be doin wrong
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
If you aren't seeing "Failed to startActivity errorMsg=..." then you must not be running the latest code.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
i dont get you am sory
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
The code above specifies the error function as:
function(errorMsg) {alert('Failed to startActivity errorMsg=' + errorMsg)}. So if the output you see doesn't also have the "errorMsg=" text, then you must not have copied, built, or installed the above code. If you use the above, then the errorMsg displayed might give us more clue as to what's wrong.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
it has that error mesage
this is the full error message

"failed to start activity errormsg=No Activity found to handle intent{act=android.intent.action.View dat=whatsapp://send?text=Hello WORLD!}

THIS IS what i did that gave me that error message
<script type="text/javascript">
function app() {
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: 'whatsapp://send?text=Hello%20World!'
},
function() {alert('success')},
function(errorMsg) {alert('Failed to startActivity errorMsg=' + errorMsg)}
);
}
</script>
<button onclick='app()'>whatsapp</button>
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Is the WhatsAp app installed?
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
no its not, could that be the reason ? and if that is, is it possible i launch apps from playstore
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Yes. Android won't know that there is an app that can handle a "whatsapp://" url unless you install that app. If you want to launch the app store instead, you might use a URL like: market://details?id=com.whatsapp according to the documentation at: http://developer.android.com/distribu...
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
all right ill try it out
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
thank u so much, it works perfectly, i think ill prefer that method, really appreciate
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
You're welcome. I suppose you could combine the two methods. First, try to start the whatsapp app. But if that fails, then start the android market.
Photo of stack stack

stack stack

  • 98 Posts
  • 0 Reply Likes
how ? that would be really gud to do, but can u show me how to go abt that
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Something like:

<script type="text/javascript"> 

function launchWhatsApp() {
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: 'whatsapp://send?text=Hello%20World!'
},
function() {alert('success')},
function(errorMsg) {
console.log('Failed to startActivity errorMsg=' + errorMsg);
launchMarketForWhatsApp();
}
);
}

function launchMarketForWhatsApp() {
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: 'market://details?id=com.whatsapp'
},
function() {alert('success')},
function(errorMsg) {
console.log('Failed to startActivity errorMsg=' + errorMsg);
}
);
}

</script>
<button onclick='launchWhatsApp()'>whatsapp</button>
Photo of Amir

Amir

  • 8261 Posts
  • 263 Reply Likes
Thanks John!
Photo of vespino

vespino

  • 132 Posts
  • 3 Reply Likes
What about iOS?
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
See your other thread.
[It's not always a great idea to ask the same question simultaneously in multiple threads]
Photo of Mayursinh Solanki

Mayursinh Solanki

  • 2 Posts
  • 0 Reply Likes
hello,
your code is working fine. but i want to open my own app so please help me?
Photo of John Weidner

John Weidner, Champion

  • 435 Posts
  • 80 Reply Likes
Check out this plugin: https://github.com/EddyVerbruggen/Cus.... I think that is what you are looking for.
Photo of Mayursinh Solanki

Mayursinh Solanki

  • 2 Posts
  • 0 Reply Likes
Hello,
i try your suggested plugin but its not working properly.

below are the code of my config.xml :

<gap:plugin name="nl.x-services.plugins.launchmyapp">

<param name="URL_SCHEME" value="stsprinter" />
</gap:plugin>

<feature name="Custom URL scheme">
<param name="id" value="nl.x-services.plugins.launchmyapp" />
<param name="url" value="https://github.com/EddyVerbruggen/LaunchMyApp-PhoneGap-Plugin.git" />
<variable name="URL_SCHEME" value="stsprinter" /><!-- change as appropriate -->
</feature>
</pre>

and my .html file code is:

<script>

function launchApp() {

window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: 'stsprinter://'
},
function() {alert('success1')},
function(errorMsg) {
alert('false')
}
);
}
</script>


Open the other app

can u please guide me for the same.

thanks in advance,
mayur