This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Device Interaction in an Ionic App with Cordova Plugins

4:59 Angular 1.x lesson by

In this lesson, we are going to learn how to interact with native components through Cordova plugins. We will walk through how to add a Cordova plugin to our application and use it to interact with our native device. In this case, we are going to install a plugin that will allow us to open URLs in a mobile browser from within our Ionic app.

  • item
  • item
Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson, we are going to learn how to interact with native components through Cordova plugins. We will walk through how to add a Cordova plugin to our application and use it to interact with our native device. In this case, we are going to install a plugin that will allow us to open URLs in a mobile browser from within our Ionic app.

  • item
  • item

In this lesson, we are going to learn how to add a Cordova plugin to our application to handle external URLs. We are presuming that you have an account with Ionic, and you have downloaded the Ionic View application to your mobile device.

So the first thing we're going to do is log in via Ionic login. I'm going to put in my username and my password. Once I am authenticated, I am going to upload my application to Ionic. We'll go Ionic upload. This will upload the latest version of the application to Ionic so we can view it in the Ionic View app.

We'll pull this up. I'm just going to log in with my account. Click log in. Then we can see the applications that I have in my account. We'll choose Eggly Ionic. Then we are going to go ahead and view the app.

Once this loads up, we are going to select a bookmark. You're going to see some interesting behavior. We'll go ahead. Let's just choose Rob Wolf. You can see that now we are in this website, but we have no way to get back to our application. So we are going to fix this by adding in a Cordova plugin to handle that.

We'll hop back into our command line, and we're going to type Ionic plugin to see the plugins that we currently have installed within our application. Now we're going to install Cordova plugin in app browser plugin. So let's type, "Ionic plugin add Cordova plugin in-app browser." This will allow us to pull up a browser window within our application.

Now that this is installed, let's do Ionic plugin one more time. You can see now that we have the in-app browser.

Now let's hop into our bookmarks list controller. We're going to add a method to our controller to navigate to a link that we click and direct it to the appropriate target. We'll go ahead and create a method called go to URL. We'll pass in the bookmark that we want to navigate to. From here, we're going to go window.open. We're going to pass in the bookmark. We're going to set the target to _system.

Then, from here, we need to update our link to not use href, but rather to use ng click. We'll just take this out. We'll replace this with an ng click to the method that we just created on our controller. We'll call go to URL, and we'll pass in the bookmark.

From here, we are ready to hop back into our command line. Let's go ahead and upload the latest to Ionic. Once this is uploaded, we can go back to Ionic View. We're going to grab the latest version of this application. We're going to click Eggly Ionic.

We're going to click sync with the latest. This is going to pull the latest version down onto our mobile device. Now we can view app. From here, let's go ahead and click a link. You can see now that we're pulling up the browser, but it is within our application, and we have a way to navigate back.

So just to review what we've done, we authenticated with Ionic from the command line using Ionic login. Then we used Ionic upload to upload our application to Ionic.

Then, from Ionic View, we were able to pull up Eggly. When we clicked on a link, we noticed that we were being popped out into a browser with no way to get back.

The way that we solved that is by using Ionic plugin add and the plugin we wanted. In this case, it was Cordova plugin in-app browser. Then we were able to see that being installed with Ionic plugin. This is how you add a Cordova plugin to your application.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?