@speigg/nativescript-urlhandler
为您的 NativeScript 应用注册自定义 URL
npm i --save @speigg/nativescript-urlhandler

NativeScript URL 处理器插件

Greenkeeper badge Build Status npm version

NPM

请随意 捐赠

点击此处支持并捐款至 www.pledgie.com ! 或捐赠 比特币

也可以通过 greenaddress 进行。

使用方法

只需将应用程序链接添加到您的应用程序中,查看以下 iOS 和 Android 指令,并为 URL 数据注册一个处理器。

请参考以下 Angular 示例

import { Component } from "@angular/core";
import { handleOpenURL, AppURL } from 'nativescript-urlhandler';

@Component({
selector: "gr-main",
template: "<page-router-outlet></page-router-outlet>"
})
export class AppComponent {
constructor() {
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
});
}
}

以及纯 NativeScript

var handleOpenURL = require("nativescript-urlhandler").handleOpenURL;

handleOpenURL(function(appURL) {
console.log('Got the following appURL', appURL);
});

或者作为 TypeScript

import { handleOpenURL, AppURL } from 'nativescript-urlhandler';

handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
});

安装

$ tns plugin add nativescript-urlhandler

或者如果您想使用开发版本(夜间构建),这可能不稳定!

$ tns plugin add nativescript-urlhandler@next

Android

myapp 替换为您想要的方案并设置 launchMode 为 singleTask

<activity android:name="com.tns.NativeScriptActivity" ... android:launchMode="singleTask"...>
...
<intent-filter>
<data android:scheme="myapp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

例如

<activity android:name="com.tns.NativeScriptApplication" android:label="@string/app_name" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="__PACKAGE__" />
</intent-filter>
</activity>

android:launchMode="singleTask" 告诉 Android 操作系统使用新的 activity 实例启动应用程序,或者使用现有的一个。如果没有这个,您的应用程序将启动多个实例,这不好。

iOS

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.yourcompany.myapp</string>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>