简体   繁体   English

是否可以实现从网站到本机应用程序的深层链接?

[英]Is it possible to implement deep links from website to native app?

I have three buttons on my website, that link to Facebook, Twitter & vk.com pages. 我的网站上有三个按钮,链接到Facebook,Twitter和vk.com页面。 I want to open native app, if it is installed on user device. 我想打开本机应用程序,如果它安装在用户设备上。 Otherwise, I want URL fallback to be opened. 否则,我希望打开URL后备。

First of all, I tried to use native app schemes directly with deep-link.js plugin. 首先,我尝试使用deep-link.js插件直接使用本机应用程序方案。 But, when I tried to open native app URL scheme, when native app was not installed, Safari has shown an error, but opened URL fallback page finally. 但是,当我尝试打开本机应用程序URL方案时,如果未安装本机应用程序,Safari会显示错误,但最终会打开URL回退页面。 Default Android browser said that he does not know how to handle such URL scheme: 默认Android浏览器说他不知道如何处理这样的URL方案:

 <a class="btn btn-primary" href="https://www.facebook.com/warpcompany" data-app-ios="fb://profile/838619192839881" data-app-android="fb://page/838619192839881">Facebook</a> 

Then I tried to use App Links "standard", that that has so much promotion from Facebook. 然后我尝试使用App Links“标准”,这有很多来自Facebook的推广。 I even tried to use their hosted app links, to make sure I've generated everything right way. 我甚至尝试使用他们的托管应用程序链接,以确保我已经正确地生成了一切。 It does not work, it always redirect to website fallback. 它不起作用,它总是重定向到网站后备。 You can easily test it by yourself from https://fb.me/746134728830806 您可以从https://fb.me/746134728830806轻松地自行测试

Is it possible to provide deep link on website, that will open native app without errors at least in default os browsers, or fallback silently to URL? 是否有可能在网站上提供深层链接,这将至少在默认的os浏览器中打开原生应用程序而不会出错,或者无声地回退到URL?

It is still possible, but on newer versions of the Android default browser you have to use intents instead of just trying to open the deep link. 它仍有可能,但在Android默认浏览器的较新版本中,您必须使用意图而不是仅仅尝试打开深层链接。 For example replace your fb://page/838619192839881 with 例如,用你的fb://page/838619192839881替换

intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;end

This will fallback to Google play by default, but you can override the fallback adding a S.browser_fallback_url : 默认情况下,这将回S.browser_fallback_url Google Play,但您可以覆盖添加S.browser_fallback_url的后备:

intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;S.browser_fallback_url=http%3A%2F%2Fwww.facebook.com%2F;end

The fallback should be url encoded. 后备应该是url编码的。

Of course you'll have issues if the user is not on an Android phone or with an old version of the default browser (or strange browser). 当然,如果用户不在Android手机上或使用旧版本的默认浏览器(或奇怪的浏览器),您将遇到问题。 You can setup a bunch of conditions and replace your HTML with the correct code for each case. 您可以设置一系列条件,并使用每个案例的正确代码替换您的HTML。

I work on HOKO , which is a free, simple and easy way to have everything working on Android and iPhone and we've done all the dirty work around this. 我在HOKO上工作,这是一种免费的,简单易用的方法,让所有东西都能在Android和iPhone上工作,我们已经完成了所有肮脏的工作。 You can rather use it or just drop us a line and we'll help you out. 你可以宁可使用它,也可以直接告诉我们,我们会帮助你。

The accepted answer will only work on Chrome v28 and default browsers for Android 5.0. 接受的答案仅适用于Chrome v28和Android 5.0的默认浏览器。 If you want this to work on other browser like Facebook/Twitter webviews, Firefox, UC and older default browsers than 5.0, you'll need to use some code that's a little more complicated. 如果你想在其他浏览器上工作,比如Facebook / Twitter webviews,Firefox,UC和5.0之前的旧版默认浏览器,你需要使用一些更复杂的代码。

Add this function to your JS snippet: 将此函数添加到您的JS代码段:

    var openSesame = function() {
        var method = 'iframe';
        var fallbackFunction = function() {
            if (method == 'iframe') {
                window.location = "market://details?id=com.facebook.katana";
            }
        };
        var addIFrame = function() {
            var iframe = document.createElement("iframe");
            iframe.style.border = "none";
            iframe.style.width = "1px";
            iframe.style.height = "1px";
            iframe.src = "fb://page/838619192839881";
            document.body.appendChild(iframe);
        };
        var loadChromeIntent = function() {
            method = 'intent';
            document.location = "intent://page/838619192839881#Intent;scheme=fb;package=com.facebook.katana;end";
        };
        if (navigator.userAgent.match(/Chrome/) && !navigator.userAgent.match("Version/")) {
            loadChromeIntent();
        }
        else if (navigator.userAgent.match(/Firefox/)) {
            window.location = "fb://page/838619192839881";
        }
        else {
            addIFrame();
        }
        setTimeout(fallbackFunction, 750);
    };

Then your button will look like this: 然后你的按钮看起来像这样:

<a href="" onclick="openSesame();">Open the App</a>

Or you can use a service like branch.io which does exactly what you're looking for automatically. 或者您可以使用像branch.io这样的服务,它可以自动完成您正在寻找的服务。

Yes it is! 是的! Have a look to this training: https://developer.android.com/training/app-indexing/deep-linking.html 看看这个培训: https//developer.android.com/training/app-indexing/deep-linking.html

Is this the information you needed? 这是您需要的信息吗? lmk LMK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM