tns-urihandler
Nativescript 内部应用通信 URI 处理模块。
npm i --save tns-urihandler

tns-urihandler

Nativescript 内部应用通信 URI 处理模块

如何在 NativeScript 项目中自定义协议

Android
将以下代码添加到您的 AndroidManifest.xml 文件中,您可以在 /app/App_Resources/Android/AndroidManifest.xml 中找到它

<activity android:launchMode="singleTop">

<!-- Copy from here -->
<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="yourscheme" />
</intent-filter>
<!-- Copy end here -->

</activity>

注意:属性 android:launchMode="singleTop" 阻止操作系统启动多个活动。

iOS
将以下代码添加到您的 Info.plist 文件中,您可以在 /app/App_Resources/iOS/Info.plist 中找到它

<dict>
<!-- Copy from here -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>your.app.id</string>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>yourscheme</string>
</array>
</dict>
</array>
<!-- Copy end here -->
</dict>

注意:在修改 AndroidManifest.xml 或 Info.plist 之后,请删除整个平台并重新添加平台。官方文档也推荐这样做。

如何安装插件?

运行命令

    tns plugin add tns-urihandler

    tns plugin add tns-urihandler-1.0.0.tgz

如何使用插件?

传入的 URI:myapp://Jacky:Passw0rd@home/path/to/my/page?from=anotherApp#fragment

    import { URIHandler } from 'tns-urihandler';

export class MyApp{
foo(): void{
let uriHandler = new URIHandler();
console.log(uriHandler.getURI()); // myapp://Jacky:Passw0rd@home/path/to/my/page?from=anotherApp#fragment
}
}

APIs

    uriHandler.getURI();            // myapp://Jacky:Passw0rd@home/path/to/my/page?from=anotherApp#fragment
uriHandler.getHost(); // home
uriHandler.getFragment(); // fragment
uriHandler.getScheme(); // myapp
uriHandler.getQuery(); // from=anotherApp
uriHandler.getPath(); // path/to/my/page
uriHandler.getUser(); // Jacky (if there's any Username)
uriHandler.getPassword(); // Passw0rd (if there's any Password)

// experimental function, returns object
uriHandler.search(); // { form: 'antherApp' }

感谢

我要感谢 hypery2k 的 nativescript-urlhandler。我的灵感来源于他的出色工作。