├── open-app.d.ts ├── package.json ├── open-app.android.js ├── open-app.ios.js ├── LICENSE └── README.md /open-app.d.ts: -------------------------------------------------------------------------------- 1 | declare module "nativescript-open-app" { 2 | /** 3 | * Open another application on the device. 4 | */ 5 | export function openApp(appID: string, storeFallback?: boolean, appleStoreId?: string): boolean; 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nativescript-open-app", 3 | "version": "0.3.0", 4 | "description": "A NativeScript plugin for opening other applications on the device (with app store fallback).", 5 | "main": "open-app", 6 | "typings": "./open-app.d.ts", 7 | "nativescript": { 8 | "platforms": { 9 | "ios": "2.5.0", 10 | "android": "2.5.0" 11 | } 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/ludwiktrammer/nativescript-open-app.git" 16 | }, 17 | "keywords": [ 18 | "NativeScript", 19 | "Open", 20 | "Open App", 21 | "Android", 22 | "iOS", 23 | "Launch", 24 | "Start" 25 | ], 26 | "author": "Ludwik Trammer ", 27 | "license": "MIT", 28 | "bugs": "https://github.com/ludwiktrammer/nativescript-open-app/issues", 29 | "homepage": "https://github.com/ludwiktrammer/nativescript-open-app" 30 | } 31 | -------------------------------------------------------------------------------- /open-app.android.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var utils = require("utils/utils"); 3 | 4 | function openApp(appID, storeFallback, appleStoreId) { 5 | if (storeFallback === void 0) { storeFallback = true; } 6 | var context = utils.ad.getApplicationContext(); 7 | var Intent = android.content.Intent; 8 | var intent = context.getPackageManager().getLaunchIntentForPackage(appID); 9 | if (intent) { 10 | // Open app 11 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 12 | context.startActivity(intent); 13 | return true; 14 | } 15 | else if (storeFallback) { 16 | // Can't open app - open store 17 | intent = new Intent(Intent.ACTION_VIEW); 18 | intent.setData(android.net.Uri.parse("https://play.google.com/store/apps/details?id=" + appID)); 19 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 20 | context.startActivity(intent); 21 | } 22 | return false; 23 | } 24 | exports.openApp = openApp; 25 | -------------------------------------------------------------------------------- /open-app.ios.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var utils = require("utils/utils"); 3 | 4 | function openApp(appID, storeFallback, appleStoreId) { 5 | if (storeFallback === void 0) { storeFallback = true; } 6 | var sharedApplication = UIApplication.sharedApplication; 7 | var url = NSURL.URLWithString(appID.trim()); 8 | if (sharedApplication.canOpenURL(url)) { 9 | // open app 10 | sharedApplication.openURL(url); 11 | return true; 12 | } 13 | else if (storeFallback && appleStoreId) { 14 | // can't open app - open store 15 | url = NSURL.URLWithString("itms-apps://itunes.apple.com/app/id" + appleStoreId); 16 | if (sharedApplication.canOpenURL(url)) { 17 | sharedApplication.openURL(url); 18 | } 19 | else { 20 | // can't open store - open the website 21 | url = NSURL.URLWithString("https://itunes.apple.com/app/id" + appleStoreId); 22 | sharedApplication.openURL(url); 23 | } 24 | } 25 | return false; 26 | } 27 | exports.openApp = openApp; 28 | -------------------------------------------------------------------------------- /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. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NativeScript Open App plugin 2 | A NativeScript plugin for opening other applications on the device (with app store fallback) 3 | 4 | 5 | ## Installation 6 | Run the following command from the root of your project: 7 | 8 | ``` 9 | tns plugin add nativescript-open-app 10 | ``` 11 | 12 | ## Usage 13 | ### Android 14 | To open an app, you need its id (for example "com.facebook.katana" for Facebook and "com.twitter.android" for Twitter). 15 | You can easily find it in app's URL on https://play.google.com. 16 | 17 | #### Examples 18 | 19 | Open Facebook app if it is installed on the device (open Facebook app's entry in Play Store otherwise): 20 | ``` 21 | var openApp = require("nativescript-open-app").openApp; 22 | 23 | var installed = openApp("com.facebook.katana"); 24 | console.log("Is it installed? " + installed); 25 | ``` 26 | 27 | Open Facebook app if it is installed on the device (do nothing otherwise): 28 | ``` 29 | var openApp = require("nativescript-open-app").openApp; 30 | 31 | var installed = openApp("com.facebook.katana", false); 32 | console.log("Is it installed? " + installed); 33 | ``` 34 | 35 | Open Facebook app if it is installed on the device (open facebook.com otherwise): 36 | ``` 37 | var openApp = require("nativescript-open-app").openApp; 38 | var utils = require("utils/utils"); 39 | 40 | var installed = openApp("com.facebook.katana", false); 41 | if (!installed) { 42 | utils.openUrl("https://facebook.com"); 43 | } 44 | ``` 45 | 46 | Simple TypeScript example: 47 | ``` 48 | import { openApp } from "nativescript-open-app"; 49 | 50 | openApp("com.facebook.katana"); 51 | ``` 52 | 53 | ### iOS 54 | To open an app on iOS you need a schema registered by the app. 55 | Additionally you are required to whitelist the schemas for all apps you want to be able to open. Add them to your `app/App_Resources/iOS/Info.plist` (and additionally include "itms-apps" schema used by the App Store): 56 | 57 | ``` 58 | LSApplicationQueriesSchemes 59 | 60 | itms-apps 61 | twitter 62 | 63 | ``` 64 | 65 | #### Examples 66 | Open Facebook app if it is installed on the device (do nothing otherwise): 67 | ``` 68 | var openApp = require("nativescript-open-app").openApp; 69 | 70 | var installed = openApp("fb://"); 71 | console.log("Is it installed? " + installed); 72 | ``` 73 | 74 | Open Facebook app if it is installed on the device (open Facebook app's entry in App Store otherwise). 75 | The third argument to `openApp` is the app's id in App Store (you can easily find it in app's URL on https://itunes.apple.com): 76 | ``` 77 | var openApp = require("nativescript-open-app").openApp; 78 | 79 | var installed = openApp("fb://", true, "284882215"); 80 | console.log("Is it installed? " + installed); 81 | ``` 82 | 83 | Open Facebook app if it is installed on the device (open facebook.com otherwise): 84 | ``` 85 | var openApp = require("nativescript-open-app").openApp; 86 | var utils = require("utils/utils"); 87 | 88 | var installed = openApp("fb://", false); 89 | if (!installed) { 90 | utils.openUrl("https://facebook.com"); 91 | } 92 | ``` 93 | 94 | Simple TypeScript example: 95 | ``` 96 | import { openApp } from "nativescript-open-app"; 97 | 98 | openApp("fb://"); 99 | ``` 100 | --------------------------------------------------------------------------------