如何从WebView打开Android App链接 [英] How to open an Android App link from a webview

查看:230
本文介绍了如何从WebView打开Android App链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以处理应用程序链接的应用程序。

I have an app that can handle app links.

例如,如果您有一个链接,例如 https://my-app-domain.com/something ,然后单击它,它将启动我的应用程序。

For example, if you have a link such as https://my-app-domain.com/something and you click on it, it would launch my app.

但是,如果链接是在打开了网络视图中链接的应用程序中发送的,则我的应用程序将无法启动。例如,Facebook Messenger,Instagram和Snapchat都在自己的Web视图中打开链接,这些链接将用户带到我的网站,而不是启动我的应用程序。

However, if the link was sent in an app that opens links in a webview, my app won't be launched. For example, Facebook Messenger, Instagram and Snapchat all open links in their own webview which takes the user to my website instead of launching my app.

我要做的是使此链接启动我的应用程序,即使它是在打开Web视图中链接的应用程序中发送的。

What I'm trying to do is make this link launch my app even it was sent in an app that opens links in a webview.

谢谢

推荐答案

就像您说的那样,Facebook Messenger不会t处理应用程序链接/通用链接。

Like you say, Facebook messenger doesn't handle App links/universal links.

我已经做了一些尝试,似乎自定义uri方案样式链接(my-app:// something)可以工作。您可以做的是在 https://my-app-domain.com/something上实施网络后备尝试将浏览器重定向到您的自定义uri,如果这样做不起作用,则显示一个漂亮的网页。

I've been experimenting a bit and it seems like custom uri scheme style links (my-app://something) work. What you can do is to implement a web fallback on https://my-app-domain.com/something which tries to redirect the browser to your custom uri, and if this doesn't work, display a nice web page. This is how big companies such as Spotify does it.

在Android上,您可以通过指定多个intent-filters来支持应用程序链接和自定义uri方案;

On Android you can support both app links and a custom uri scheme by specifying multiple intent-filters;

    <intent-filter android:autoVerify="true">
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />

      <data android:scheme="https"
            android:host="my-app-domain"
            android:pathPrefix="/something" />
    </intent-filter>
    <!-- Google claims that one intent-filter can handle multiple <data> elements, this seems to be untrue however -->
    <intent-filter android:autoVerify="true">
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />

      <data android:scheme="my-app" />
    </intent-filter>

然后在 https://my-app-domain.com/something ,您就会遇到这种令人讨厌的骇客。

Then on your web fallback at https://my-app-domain.com/something, you would have this disgusting hack.

<script>
    window.location = 'my-app://something'
    // optional secondary fallback
    setTimeout(() => {window.location = 'https://secondary-fallback'}, 1000)
</script>

结果是,如果安装了该应用程序,则最终会按预期出现在您的应用程序中,但是如果否则,您最终会进入辅助后备页面。

The result is that if you have the app installed you end up in your app as expected, but if you don't, you end up at your secondary fallback page.

同样的原理也适用于iOS。我使用的是react native,并在AppDelegate.m中添加了以下内容:

The same principle works for iOS too. I'm using react native and there I've added the following in my AppDelegate.m:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                  sourceApplication:sourceApplication annotation:annotation];
}

然后在Info.plist中指定uri方案:

And then specify a uri scheme in Info.plist:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>my-app</string>
        </array>
    </dict>
</array>

这篇关于如何从WebView打开Android App链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆