【问题标题】:How to open an Android App link from a webview如何从 web 视图打开 Android 应用程序链接
【发布时间】:2020-03-27 01:35:29
【问题描述】:

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

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

但是,如果该链接是在一个在 web 视图中打开链接的应用程序中发送的,我的应用程序将不会启动。例如,Facebook Messenger、Instagram 和 Snapchat 都在他们自己的 web 视图中打开链接,将用户带到我的网站,而不是启动我的应用程序。

我想要做的是让这个链接启动我的应用程序,即使它是在一个在 web 视图中打开链接的应用程序中发送的。

谢谢,

【问题讨论】:

    标签: android webview deep-linking applinks


    【解决方案1】:

    正如你所说,Facebook Messenger 不处理应用链接/通用链接。

    我一直在尝试,似乎自定义 uri 方案样式链接 (my-app://something) 有效。您可以做的是在https://my-app-domain.com/something 上实现网络回退,它会尝试将浏览器重定向到您的自定义uri,如果这不起作用,请显示一个漂亮的网页。这就是 Spotify 等大公司的做法。

    在 Android 上,您可以通过指定多个意图过滤器来支持应用链接和自定义 uri 方案;

        <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 中,您会遇到这种令人作呕的黑客攻击。

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

    结果是,如果您安装了应用程序,您最终会按预期进入您的应用程序,但如果您没有安装,您最终会进入辅助后备页面。

    同样的原则也适用于 iOS。我正在使用本机反应,并在我的 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 scheme:

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

    【讨论】:

    • 网络后备到底是什么意思?
    • 您通常会将应用链接/通用链接指向您拥有的域。在此域上,您将托管一个简单的网页。此网页是网页后备。如果您在未安装相关应用程序的设备上点击通用链接(或者如果您在例如 PC 上点击该链接),那么您最终会到达这里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    相关资源
    最近更新 更多