cordova 如何从 http 或 https url 打开应用程序?

how can cordova open app from http or https url?

我找到了很多这样的自定义 URL-Scheme 的答案 (mycoolapp://somepath)。

This plugin 例如添加自定义 URL-Sheme.*

但我不想要自定义 URL-Scheme,我想要这样的 "normal" URL (http://www.mycoolapp.com/somepath).

例如,如果您在浏览器中打开它或单击 Hyperlink,那么它应该会要求您打开我的应用程序(就像 google 地图那样)。

这个问题可能已经有了答案,但我找不到。

如果您不明白我的意思,如果您在 Android 设备上点击 link 进入我的网站,它应该是这样的:

只需使用我的应用即可 select。

您需要做的是检测连接到 http://www.mycoolapp.com/somepath

的设备

如果它是移动设备,那么您可以向他们展示一个带有 link 的页面,您的自定义 url 方案可以打开您的应用程序。或根据需要自动打开应用程序的自定义 url。

您应该将 intent-filter 添加到 android 清单中的 activity。像这样:

<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="http" />
   <data android:host="www.mycoolapp.com" />
   <data android:pathPrefix="/somepath" />
</intent-filter>

更多关于 data 您可以在此处添加的内容:http://developer.android.com/guide/topics/manifest/data-element.html

在 Whosebug 上还有更多 here...

对于同样的问题,我使用了现有的 webintent 插件,修改了 android 清单文件 - 将这些行添加到 activity

<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:host="example.com" android:scheme="http" />
</intent-filter>

并修改了 index.html ondeviceready:

function deviceReady() {
    window.plugins.webintent.getUri(function(url) {
        console.log("INTENT URL: " + url);
        //...
    }); 
}

编辑

我刚刚注意到一个可能不受欢迎的行为。当您使用来自另一个应用程序的 link(意图)打开应用程序时,它将(在许多情况下)创建一个新实例而不使用已经 运行 的实例(已通过 gmail 和 skype 测试)。为防止这种情况,解决方案是更改 config.xml 文件中的 Android 启动模式:

<preference name="AndroidLaunchMode" value="singleTask" />

(它适用于 cordova 3.5,不确定旧版本)

那你还需要在ondeviceready中增加一个函数:

window.plugins.webintent.onNewIntent(function(url) {
    console.log("INTENT onNewIntent: " + url);
});

当应用程序已经 运行 并且有意图地被带到前面时触发这个。

您要找的是 iOS 上的 "Universal Links" 和 Android 上的 "Deep Linking"。

并且有一个 Cordova 插件可以处理:https://www.npmjs.com/package/cordova-universal-links-plugin

对于希望使用 ,但通过 config.xml 修改 AndroidManifest 的任何人来说,以下方法对我有用。无论我使用哪种排列,尝试匹配 android:name 都不起作用。

        <config-file target="AndroidManifest.xml" parent="/manifest/application/activity[@android:label='@string/activity_name']">
            <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="http" />
                <data android:scheme="https" />
                <data android:host="www.mysite.com" />
                <data android:pathPrefix="/whatever/path" />
            </intent-filter>
        </config-file>