Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » Cordova InAppBrowser Plugin Example using ionic framework

Cordova InAppBrowser Plugin Example using ionic framework

October 15, 2015 by Source Freeze 18 Comments

In the previous tutorial  i had written a post about how to use cordova device plugin, this post was about cordova inappbrowser plugin example using ionic framework and ngcordova.

the cordova inappbrowser plugin is used to launch the URL into external browser or external applications, this example will work on iOS and Android devices.

Next create the ionic cordova project using the below command lines.

ionic start InAppBrowser blank

cd InAppBrowser

Then add the android and iOS platforms using the below ionic commands.

ionic platform add android

ionic platform add ios

Note:

Remember you must have mac to build ios applications.

Adding Cordova InAppBrowser Plugin

Next we have add the apache cordova inappbrowser plugin using the below command line.

ionic plugin add cordova-plugin-inappbrowser

Now we have configured the application to use cordova inappbrowser plugin.

Then add the buttons for open the link into various types browser client in the ion-content tag. once added the button tags add the event using ng-click attribute and add ng-controller attribute in the ion-content as mentioned in the below.

<body ng-app="starter">
    <ion-header-bar class="bar-stable">
        <h1 class="title">InAppBrowser</h1>
    </ion-header-bar>
    <ion-content ng-controller="ExampleController" padding="true">
        <button class="button button-full button-assertive" ng-click="openInExternalBrowser()">
            Open In External Browser
        </button>
        <button class="button button-full button-assertive" ng-click="openInAppBrowser()">
            Open In App Browser
        </button>
         <button class="button button-full button-assertive" ng-click="openCordovaWebView()">
            Open In Cordova Webview
        </button>
    </ion-content>
</body>

InAppBrowser Function

cordova inappbrowser plugin provides a web browser view that displays when calling  cordova.InAppBrowser.open() or existing window.open().

_blank: It opens the URL in the in app browser
_self: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the InAppBrowser.
_system: Opens in the system’s web browser.
location=yes: It displays the full URL of the page in the InAppBrowser in a location bar, by the default location is set to no.

angular.module('starter', ['ionic'])

.controller("ExampleController", function ($scope) {

$scope.openInExternalBrowser = function()
{
 // Open in external browser
 window.open('http://google.com','_system','location=yes'); 
};

$scope.openInAppBrowser = function()
{
 // Open in app browser
 window.open('http://google.com','_blank'); 
};

$scope.openCordovaWebView = function()
{
 // Open cordova webview if the url is in the whitelist otherwise opens in app browser
 window.open('http://google.com','_self'); 
};

});

next we have to run the application using the below command lines. if you are running in emulator you can use emulate instead run.

ionic run ios

ionic run android

Then run the application the output will looks like below, and find the full source code for this application in this github link.

cordova inappbrowser plugin using ionic

Filed Under: cordova, Hybrid, ionic framework, phonegap

Comments

  1. chris says

    November 16, 2015 at 6:35 pm

    so once i run this should it open the link in the emulator? using visual studio and have the correct output with the three buttons but when i click nothing happens……..

    Reply
  2. ac says

    December 15, 2015 at 9:00 pm

    Nothing happens on click…did you test run these steps.

    Reply
  3. yustein says

    December 23, 2015 at 6:00 pm

    It worked for me, check your declarations in the app for the controller. Depending on the app template you are using, controllers might be tied to your views in a different way.

    Reply
  4. Tae Hwa Kim says

    January 5, 2016 at 4:43 pm

    Hello. I have a question. Do I need ngCordova? Without ngCordova, it works fine on iOS but not on android.
    The problem of android is opening a pdf file. When I open a pdf file, it opens just empty webview. After while, I realised I did not installed ngCordova, and I installed but still not working on android.

    This is a test js.
    $scope.justOpen = function(){
    window.open(‘http://www.i-drain.net/userfiles/file/einbauanleitung_iboard.pdf’, ‘_blank’, ‘location=yes’);
    }

    And html
    |a ng-click=”justOpen();”| test |/a|

    Reply
    • sourcefreeze says

      January 14, 2016 at 6:35 pm

      Hi,
      No, you don’t need ng-cordova, please check the code once once again, still not works please let me know. i have tested in android it is working fine.

      Reply
  5. mrkazep says

    January 29, 2016 at 3:48 pm

    how to show website in ionic content ?

    Reply
  6. Gopi Nath says

    March 31, 2016 at 10:54 am

    How to add loader here…

    Reply
  7. Mahendra Rajdhami says

    June 18, 2016 at 12:35 am

    It works fine. Thanks for great tutorial. Now i want to change color of status bar so that it match with app statusbar color. Please need help, How can i do that?

    Reply
  8. Ruskov says

    July 5, 2016 at 4:29 pm

    Merci !! Ca m’a beaucoup aidé

    Reply
  9. priya says

    July 19, 2016 at 12:30 pm

    how to return back to app from ios browser?

    Reply
  10. Kit Walker says

    September 10, 2016 at 7:53 am

    From an Inappbrowser how does one browser and upload files…

    Reply
  11. Eugene says

    September 12, 2016 at 1:42 am

    Thanks for your work! Everything works very helpful during development.

    Reply
  12. Dharmendra Sharma says

    October 17, 2016 at 7:29 am

    How Can I keep left menu bar and header area?

    Reply
  13. Muhsin says

    December 31, 2016 at 7:45 am

    How do i customise web page not available message (load error) pleas help https://uploads.disquscdn.com/images/ea6a3f273754efae59fa41b7abdae3809ce84159b27dbe442a0c79793934f0c7.png

    Reply
    • SourceFreeze says

      January 3, 2017 at 9:11 am

      Hi

      Get network request if it 404 error, then redirect that page with offline HTML page.

      Reply
      • Muhsin says

        January 3, 2017 at 9:58 am

        Thanks for your replay
        But the redirect works only after coming back from the error page (from inappbrowser error page)
        i want to customise inapp browser error page

        https://uploads.disquscdn.com/images/ea6a3f273754efae59fa41b7abdae3809ce84159b27dbe442a0c79793934f0c7.png?w=800&h=1331

        or help me to hide the url in error page

        please help

        Reply
        • SourceFreeze says

          January 3, 2017 at 10:06 am

          Did you tried loadErrorCallBack event in app browser plugin? try to get network details from that event then proceed to show offline page or whatever you want.

          Reply
          • Muhsin says

            January 3, 2017 at 11:08 am

            $scope.login = function() {
            $cordovaOauth.facebook(“”, [“email”, “public_profile”]).then(function(result) {
            result.access_token;

            }, function(error) {

            });
            };

            This is my code where do i call loadErrorCallBack pleas explain

            Thank you

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development nextjs objective-c swift uiwebview Visual Studio plugin

Copyright © 2023 Source Freeze