Branch.io: javascript 检测是否安装了移动应用

Branch.io: javascript detect whether mobile app is installed

我有一个网页,如果安装了应用程序,我希望页面上的特定 link 打开我们的本机移动应用程序,如果没有,则执行当前正在执行的操作(提交表单).

注意:我认为这与智能横幅不同 - 我不想要此页面上的横幅。如果没有移动应用程序,我只想要正常的应用程序流程。

我已将 branch-sdk 集成到网页和我的 ios 应用程序中。我已经成功地设置了从网页到 iOS 应用程序的深层 link(代码未显示),但是在嗅探应用程序是否已安装时,我没有得到预期的结果。

这是我网页 <head> 中的代码:

    branch.init('MY_KEY', null, function(err, data) {
        console.log('init...');
        console.dir(data);
    });
    branch.setIdentity('test-user', function(err, data) {
        console.log('identity...');
        console.dir(data);
    });
    branch.data(function(err, data) {
        console.log('data...');
        console.dir(data);
    })

这是我在 iOS 端的 application(_:didFinishLaunchingWithOptions:) 中的代码:

    //Initialize Branch
    if let branch = Branch.getInstance() {
        branch.setDebug()
        branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: { params, error in
            if let params = params, error == nil {
                // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
                // params will be empty if no data found
                // ... insert custom logic here ...
                print("params: %@", params.description)
            }
        })
        branch.setIdentity("test-user")
    } else {
        readerLog.error("failed to initialize branch")
    }

每次我加载网页时(即使在加载之后,并遵循深度 link),我都会在控制台中得到以下输出。注意 +isfirstsession 为真, hasApp 属性 为 null:

[Log] init... (localhost, line 18)
[Log] Object (localhost, line 20)
    data: "{\"+is_first_session\":true,\"+clicked_branch_link\":false}"
    data_parsed: {+is_first_session: true, +clicked_branch_link: false}
    has_app: null
    identity: null
    referring_identity: null
    referring_link: null

[Log] identity... (localhost, line 23)
[Log] Object (localhost, line 25)
    identity_id: "352945822373397525"
    link: "https://nlfd.app.link/?%24identity_id=352945822373397525"
    link_click_id: "352947632809063724"
    referring_data: "{\"$one_time_use\":false,\"+click_timestamp\":1485387510,\"_branch_match_id\":\"352945819276765390\",\"_t\":\"352945819276765390\",\"referrer\":\"link_clic…"
    referring_data_parsed: Object
    session_id: "352945822337503746"

[Log] data... (localhost, line 28)
[Log] Object (localhost, line 30)
    data: "{\"+is_first_session\":true,\"+clicked_branch_link\":false}"
    data_parsed: null
    has_app: null
    identity: null
    referring_identity: null
    referring_link: null

我做错了什么?我希望我可以在初始化后或设置身份后查看 has_app 属性。在这种情况下假设 has_app 会 return 是不正确的吗?有没有更合适的方式做我想做的事?

A​​lex 来自 Branch.io 这里:

不幸的是,has_app 参数不是特别可靠。它足以在显示 OpenInstall 标签之间切换按钮,但理想情况下您不想将它用于功能性的东西。这是由 iOS 引起的限制:Apple 不允许网页查询设备上安装了哪些应用程序(出于明显的隐私原因),因此 Branch 必须依赖 cookie 匹配。这意味着如果我们无法匹配 cookie,或者最近没有看到用户,或者用户清除了他们的设备缓存,或者用户在 Branch 上次看到他们后卸载了应用程序,has_app 将是不正确的。

但是,即使 Apple 不允许网页查询访问此数据,iOS 本身仍然可以对其进行操作。 Universal Links 正是这样做的——当用户打开 Universal Link(其中包括 Branch links,假设您已完成所有配置)时,如果安装了该应用程序,该应用程序将打开.如果是没有安装,用户会被传送到URL的link。您只需要在该按钮后面放置一个 Branch link。

但是请注意,这不适用于表单提交按钮,因此您需要为此想出一些 UX 解决方法。或者,如果应用程序未打开,您可能会找到一种在延迟后提交表单的方法,方法是使用 Javascript 计时器。