├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── appavailability.android.js ├── appavailability.ios.js ├── index.d.ts └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NativeScript AppAvailability 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![Downloads][downloads-image]][npm-url] 5 | [![Twitter Follow][twitter-image]][twitter-url] 6 | 7 | [npm-image]:http://img.shields.io/npm/v/nativescript-appavailability.svg 8 | [npm-url]:https://npmjs.org/package/nativescript-appavailability 9 | [downloads-image]:http://img.shields.io/npm/dm/nativescript-appavailability.svg 10 | [twitter-image]:https://img.shields.io/twitter/follow/eddyverbruggen.svg?style=social&label=Follow%20me 11 | [twitter-url]:https://twitter.com/eddyverbruggen 12 | 13 | A plugin to check for availability of other apps on the device. 14 | 15 | > ⚠️ Looking for NativeScript 7 compatibilty? Go to [the NativeScript/plugins repo](https://github.com/NativeScript/plugins/tree/master/packages/appavailability). 16 | 17 | ## Installation 18 | Run the following command from the root of your project: 19 | 20 | ``` 21 | tns plugin add nativescript-appavailability 22 | ``` 23 | 24 | ## Usage 25 | 26 | > Note that version 1.3.0 added a synchronous version of this method that doesn't return a Promise. Need that? Use `availableSync` instead of `available`. 27 | 28 | ### TypeScript 29 | ```typescript 30 | const isAppAvailable = require("nativescript-appavailability").available; 31 | 32 | // examples of what to pass: 33 | // - for iOS: "maps://", "twitter://", "fb://" 34 | // - for Android: "com.facebook.katana" 35 | appavailability.available("twitter://").then((avail: boolean) => { 36 | console.log("App available? " + avail); 37 | }) 38 | ``` 39 | 40 | ### TypeScript + Angular 41 | ```typescript 42 | import * as appavailability from "nativescript-appavailability"; 43 | 44 | // examples of what to pass: 45 | // - for iOS: "maps://", "twitter://", "fb://" 46 | // - for Android: "com.facebook.katana" 47 | appavailability.available("twitter://").then((avail: boolean) => { 48 | console.log("App available? " + avail); 49 | }) 50 | ``` 51 | 52 | ### JavaScript 53 | 54 | ```js 55 | var appAvailability = require("nativescript-appavailability"); 56 | 57 | // examples of what to pass: 58 | // - for iOS: "maps://", "twitter://", "fb://" 59 | // - for Android: "com.facebook.katana" 60 | appAvailability.available("com.facebook.katana").then(function(avail) { 61 | console.log("App available? " + avail); 62 | }) 63 | ``` 64 | 65 | ## Opening an app (with web fallback) 66 | Now that you know whether an app is installed or not, you probably want to launch it. 67 | Here's a snippet that opens the mobile Twitter app and falls back to the website if it's not installed. 68 | 69 | ```typescript 70 | import { available } from "nativescript-appavailability"; 71 | import { openUrl } from "tns-core-modules/utils/utils"; 72 | 73 | const twitterScheme = "twitter://"; 74 | available(twitterScheme).then(available => { 75 | if (available) { 76 | // open in the app 77 | openUrl(twitterScheme + (isIOS ? "/user?screen_name=" : "user?user_id=") + "eddyverbruggen"); 78 | } else { 79 | // open in the default browser 80 | openUrl("https://twitter.com/eddyverbruggen"); 81 | } 82 | }) 83 | ``` 84 | 85 | And a more concise, synchronous way would be: 86 | 87 | ```typescript 88 | import { availableSync } from "nativescript-appavailability"; 89 | import { openUrl } from "tns-core-modules/utils/utils"; 90 | 91 | if (availableSync("twitter://")) { 92 | openUrl("twitter://" + (isIOS ? "/user?screen_name=" : "user?user_id=") + "eddyverbruggen"); 93 | } else { 94 | openUrl("https://twitter.com/eddyverbruggen"); 95 | } 96 | ``` 97 | 98 | ## iOS whitelisting 99 | To get useful results on iOS 9 and up you need to whitelist the URL Scheme 100 | you're querying in the application's `.plist`. 101 | 102 | Luckily NativeScript made this pretty easy. Just open `app/App_ResourcesiOS/Info.plist` 103 | and add this if you want to query for both `twitter://` and `fb://`: 104 | 105 | ```xml 106 | LSApplicationQueriesSchemes 107 | 108 | fb 109 | twitter 110 | 111 | ``` 112 | 113 | You may wonder how one would determine the correct identifier for an app. 114 | * Android: simply search the Play Store and use the id in the URL. For Twitter this is com.twitter.android because the URL is https://play.google.com/store/apps/details?id=com.twitter.android. 115 | * iOS: this one is a bit harder but this site should cover most apps you're interested in. When in doubt you can always fire up Safari on your iPhone and type for example 'twitter://' in the address bar, if the app launches you're good. 116 | -------------------------------------------------------------------------------- /appavailability.android.js: -------------------------------------------------------------------------------- 1 | exports.available = function (uri) { 2 | return new Promise(function (resolve, reject) { 3 | try { 4 | try { 5 | var pm = com.tns.NativeScriptApplication.getInstance().getPackageManager(); 6 | pm.getPackageInfo(uri, android.content.pm.PackageManager.GET_ACTIVITIES); 7 | resolve(true); 8 | } catch(e) { 9 | resolve(false); 10 | } 11 | } catch (ex) { 12 | console.log("Error in appavailability.available: " + ex); 13 | reject(ex); 14 | } 15 | }); 16 | }; 17 | 18 | exports.availableSync = function (uri) { 19 | try { 20 | try { 21 | var pm = com.tns.NativeScriptApplication.getInstance().getPackageManager(); 22 | pm.getPackageInfo(uri, android.content.pm.PackageManager.GET_ACTIVITIES); 23 | return true; 24 | } catch(e) { 25 | return false; 26 | } 27 | } catch (ex) { 28 | console.log("Error in appavailability.availableSync: " + ex); 29 | return false; 30 | } 31 | }; -------------------------------------------------------------------------------- /appavailability.ios.js: -------------------------------------------------------------------------------- 1 | exports.available = function (scheme) { 2 | return new Promise(function (resolve, reject) { 3 | try { 4 | var url = NSURL.URLWithString(scheme); 5 | var avail = UIApplication.sharedApplication.canOpenURL(url); 6 | resolve(avail); 7 | } catch (ex) { 8 | console.log("Error in appavailability.available: " + ex); 9 | reject(ex); 10 | } 11 | }); 12 | }; 13 | 14 | exports.availableSync = function (scheme) { 15 | try { 16 | var url = NSURL.URLWithString(scheme); 17 | return UIApplication.sharedApplication.canOpenURL(url); 18 | } catch (ex) { 19 | console.log("Error in appavailability.availableSync: " + ex); 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module "nativescript-appavailability" { 2 | /** 3 | * Pass in the package name as and we'll tell you whether or not the app is installed. 4 | * See the GitHub readme for details on the package name. 5 | */ 6 | export function available(packagename: string): Promise; 7 | export function availableSync(packagename: string): boolean; 8 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nativescript-appavailability", 3 | "version": "1.3.2", 4 | "description": "A NativeScript plugin to check for availability of other apps on the device.", 5 | "main": "appavailability", 6 | "typings": "index.d.ts", 7 | "nativescript": { 8 | "platforms": { 9 | "ios": "2.3.0", 10 | "android": "2.3.0" 11 | } 12 | }, 13 | "scripts": { 14 | "test": "echo 'TODO implement tests :)'" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/eddyverbruggen/nativescript-appavailability.git" 19 | }, 20 | "keywords": [ 21 | "NativeScript", 22 | "App Availability", 23 | "Availability", 24 | "Can open URL", 25 | "Check Activities", 26 | "Other app" 27 | ], 28 | "author": "Eddy Verbruggen (https://github.com/EddyVerbruggen)", 29 | "license": "MIT", 30 | "bugs": "https://github.com/eddyverbruggen/nativescript-appavailability/issues", 31 | "homepage": "https://github.com/eddyverbruggen/nativescript-appavailability" 32 | } 33 | --------------------------------------------------------------------------------