4

I have three buttons on my website, that link to Facebook, Twitter & vk.com pages. I want to open native app, if it is installed on user device. Otherwise, I want URL fallback to be opened.

First of all, I tried to use native app schemes directly with deep-link.js plugin. 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. Default Android browser said that he does not know how to handle such URL scheme:

<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. 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

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?

4

3 回答 3

9

这仍然是可能的,但在较新版本的 Android 默认浏览器上,您必须使用意图,而不是仅仅尝试打开深层链接。例如,将您的替换fb://page/838619192839881

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

默认情况下,这将回退到 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

后备应该是 url 编码的。

当然,如果用户不在 Android 手机上或使用旧版本的默认浏览器(或奇怪的浏览器),您就会遇到问题。您可以设置一堆条件,并为每种情况用正确的代码替换您的 HTML。

于 2015-07-13T09:34:36.473 回答
6

接受的答案仅适用于 Chrome v28 和 Android 5.0 的默认浏览器。如果你想让它在其他浏览器上工作,比如 Facebook/Twitter webviews、Firefox、UC 和比 5.0 更旧的默认浏览器,你需要使用一些更复杂的代码。

将此函数添加到您的 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);
    };

然后您的按钮将如下所示:

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

或者您可以使用诸如branch.io 之类的服务,它会自动完成您正在寻找的工作。

于 2015-07-15T15:32:08.390 回答
1

是的!看看这个培训: https ://developer.android.com/training/app-indexing/deep-linking.html

这是您需要的信息吗?lmk

于 2015-05-19T19:53:08.870 回答