hypery2k / cordova-urlhandler-plugin

Launch your app by a Custom URL scheme like mycoolapp://
9 stars 5 forks source link

URL Handler Plugin for Apache Cordova

Greenkeeper badge

Build Status Build status npm version Dependency Status devDependency Status

Bountysource Flattr this git repo

launch your app by a link like this: mycoolapp:// for iOS, Android and WP8 (Cordova 3.0.0+)

NPM

BEWARE: For iOS you need Cordova-iOS 3.8.0 or higher cordova platform add ios@3.8.0

Installation

$ cordova plugin add cordova-plugin-urlhandler --variable URL_SCHEME=mycoolapp

Replace mycoolapp by a nice scheme you want to have your app listen to:

Usage

1a. Your app can be launced by linking to it like this from a website or an email for example (all of these will work):

<a href="https://github.com/hypery2k/cordova-urlhandler-plugin/blob/master/mycoolapp://">Open my app</a>
<a href="https://github.com/hypery2k/cordova-urlhandler-plugin/blob/master/mycoolapp://somepath">Open my app</a>
<a href="https://github.com/hypery2k/cordova-urlhandler-plugin/blob/master/mycoolapp://somepath?foo=bar">Open my app</a>
<a href="https://github.com/hypery2k/cordova-urlhandler-plugin/blob/master/mycoolapp://?foo=bar">Open my app</a>

mycoolapp is the value of URL_SCHEME you used while installing this plugin.

1b. If you're trying to open your app from another PhoneGap app, use the InAppBrowser plugin and launch the receiving app like this, to avoid a 'protocol not supported' error:

<button onclick="window.open('mycoolapp://', '_system')">Open the other app</button>

2. When your app is launched by a URL, you probably want to do something based on the path and parameters in the URL. For that, you could implement the (optional) handleOpenURL(url) method, which receives the URL that was used to launch your app.

function handleOpenURL(url) {
  console.log("received url: " + url);
}

If you want to alert the URL for testing the plugin, at least on iOS you need to wrap it in a timeout like this:

function handleOpenURL(url) {
  setTimeout(function() {
    alert("received url: " + url);
  }, 0);
}

A more useful implementation would mean parsing the URL, saving any params to sessionStorage and redirecting the app to the correct page inside your app. All this happens before the first page is loaded.

URL Scheme hints

Please choose a URL_SCHEME which which complies to these restrictions:

TIP: test your scheme by installing the app on a device or simulator and typing yourscheme:// in the browser URL bar, or create a test HTML page with a link to your app to impress your buddies.

Description

This plugin allows you to start your app by calling it with a URL like mycoolapp://path?foo=bar ](https://build.phonegap.com/plugins))

iOS specifics

Android specifics

Development

Running integration tests

execute the runIntegrationTests.sh script for a specific platform:

PLATFORM='android' ./runIntegrationTests.sh
PLATFORM='ios' ./runIntegrationTests.sh

License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.