Code Monkey home page Code Monkey logo

cordova-plugin-browsertabs's Introduction

cordova-plugin-browsertabs

Cordova plugin to open URLs in browser tabs with access to cookies (iOS, Android)

Building

Install cordova-plugin-browsertabs plugin:

cordova plugin add https://github.com/Qbix/cordova-plugin-browsertabs.git#v1.0.0

Platforms

Usage

To open a URL in an in-app browser tab:

cordova.plugins.browsertabs.openUrl(url, {
    authSession: Boolean // >= iOS11 [iOS only]. Pass true if you need cookies, storage, etc(uses SFAuthenticationSession). Otherwise open SFSafariViewController.
    scheme: String // provide scheme if you want that plugin catch handleOpenUrl and send result in success callback 
    onOpen: Function // if present, this function is called when the browsertab is successfully opened
}, onSuccess, onError);

You can also open a URL in the system browser:

cordova.plugins.browsertabs.openUrlInBrowser(url, onSuccess, onError);

In this case, your WebView will not continue to run Javascript until you switch back to the app.

Your Cordova WebView continues to run Javascript, so you can close the tab later with:

cordova.plugins.browsertabs.close();

To make this method workable on Android we will use trick with LaunchMode "singleTask".

The user could close the browsertab manually, or you can accomplish it in the JS by navigating to closeUrl = "<scheme of app>://#put_extra_information_here" and this URL will be sent as the first parameter in onSuccess as a String.

oAuth

Typically you would use this plugin for authentication flows, to grab oAuth tokens. In this case, you will have to supply the scheme option to be used in iOS 11:

cordova.plugins.browsertabs.openUrl(url, {scheme: "myapp://", authSession:true}, onSuccess, onError);

On iOS 11 the user will have an additional dialog before the flow proceeds, telling the user your app wants to authenticate with a certain website.

Note that the oAuth flows such as facebook login will only agree to redirect you to a web url that begins with http:// or https://. That means a successful login will still leave the user looking at the browser tab. Thus, you should redirect them to your website, which should then use location.href = "myapp://something?parameters=here"; to send a message back to your Cordova app. Alternatively, you can set up Universal Links on iOS and App Links on Android to make this step more seamless (and possibly more future-proof if custom URL schemes ever go away).

In any case, you will need a plugin such as this one to run Javascript on handleOpenURL. This returns control to your WebView and you can now call close() on the previously opened browser tab.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.