【问题标题】:Applink not working inside iframe in Safari browserApplink 在 Safari 浏览器的 iframe 中不起作用
【发布时间】:2021-10-18 23:25:13
【问题描述】:

我正在尝试使用 iframe 中的 javascript 打开我的移动应用程序。 它适用于大多数设备(Android 网络浏览器、iPhone firefox、iPhone Edge) 但不适用于 iPhone Safari 浏览器。

这些是我尝试过的代码。

function openApp(){
    document.location = "myapp://someUrl"; // opening my app
}

function openAppTry2(){
    location.href = "myapp://someUrl"; // opening my app
}

如果它不在 iframe 中,它可以正常工作,但是当从 iframe 内部调用该函数时没有响应。

【问题讨论】:

    标签: javascript iframe safari applinks


    【解决方案1】:

    我参考了这些帖子,但仍然无法在 iframe 中使用应用链接。

    https://developer.apple.com/forums/thread/19928

    How to make iOS universal links work inside an iframe

    相反,我找到了使用 iframe 之外的 applink 解决此问题的其他方法。

    使用 window.addEventListener 允许 iframe 内的脚本调用位于 iframe 外的函数。

    window.addEventListener("message", callback, false); // init outside iframe
    ...
    function callback (event) {
        useData(event.data);
        document.location = "myapp://someUrl"; // opening my app
    }
    

    然后使用 window.parent.postMessage 将数据发送到 iframe 外部并调用函数

    window.addEventListener("message", setMessage, false); // init inside iframe
    ...
    window.parent.postMessage(datas); // send data to caller of iframe 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2019-11-06
      • 2018-05-02
      相关资源
      最近更新 更多