npm i --save @nativescript-community/appurl
- 版本:1.4.2
- GitHub: https://github.com/nativescript-community/appurl
- NPM: https://npmjs.net.cn/package/%40nativescript-community%2Fappurl
- 下载
- 昨日:0
- 上周:0
- 上月:0
NativeScript URL 处理器插件
这是由 nativescript-urlhandler(由 Martin Reinhardt 创建)的直接分支/重写。它似乎不再维护,并且无法按预期工作
用法
只需将 App 链接添加到您的应用中,下面有 iOS 和 Android 的说明,并注册 URL 数据的处理程序。
以下是一个 Angular 的示例
import { Component, OnInit } from "@angular/core";
import { handleOpenURL, AppURL } from '@nativescript-community/appurl';
@Component({
selector: "gr-main",
template: "<page-router-outlet></page-router-outlet>"
})
export class AppComponent {
constructor() {
}
ngOnInit(){
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
});
}
}
对于纯 NativeScript
var handleOpenURL = require("@nativescript-community/appurl").handleOpenURL;
handleOpenURL(function(appURL) {
console.log('Got the following appURL', appURL);
});
或者作为 TypeScript
import { handleOpenURL, AppURL } from '@nativescript-community/appurl';
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL', appURL);
});
注意:请查看
demo
应用以了解示例用法。首先,在应用主中添加 handleOpenURL!
安装
$ tns plugin add @nativescript-community/appurl
或者如果您想使用开发版本(夜间构建),这可能不是稳定的!
$ tns plugin add @nativescript-community/appurl@next
Android
将 myapp 替换为您希望的方案,并将启动模式设置为 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="singleTask">
<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 操作系统使用活动的新实例启动应用,或者使用现有的一个。没有这个,您的应用将启动多个实例,这是不可取的。
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>
常见问题解答
回调处理
"handleOpenURL" 回调必须在应用初始化之前调用,否则您将在控制台中看到此错误
No callback provided. Please ensure that you called "handleOpenURL" during application init!
Webpack
TypeScript 配置
如果您的 Webpack 构建失败,请尝试根据以下内容调整您的 tsconfig
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmitHelpers": true,
"noEmitOnError": true,
"lib": [
"es6",
"dom",
"es2015.iterable"
],
"baseUrl": ".",
"paths": {
"*": [
"./node_modules/@nativescript/core/*",
"./node_modules/*"
]
}
},
"exclude": [
"node_modules",
"platforms",
"**/*.aot.ts"
]