ChatGPT解决这个技术问题 Extra ChatGPT

是否可以为 iPhone 应用程序(如 YouTube 和地图)注册基于 http+域的 URL 方案?

每当手机上安装应用程序时,我想让 iOS 使用我的应用程序从我的域(例如 http://martijnthe.nl)打开 URL,如果没有,则使用 Mobile Safari。

我读到可以为此创建一个唯一的协议后缀并将其注册到 Info.plist 中,但如果未安装该应用程序,Mobile Safari 会出错。

什么是解决方法?

一个想法:

1) 使用在任何桌面浏览器中打开的 http:// URL,并通过浏览器呈现服务

2) 检查 User-Agent,如果它是 Mobile Safari,打开 myprotocol:// URL 以(尝试)打开 iPhone 应用程序并让它打开 Mobile iTunes 以下载应用程序,以防尝试失败

不确定这是否可行...建议?谢谢!

在纽约地铁,有 Boingo 的 wifi,如果您下载他们推荐的应用程序,您可以免费使用 WiFi。下载后,您将返回 Safari,浏览器会检测它是否已安装,然后授予您访问权限。知道这是怎么做的吗?
Universal Links 现在将支持此用例而不会出现任何错误消息。以下是配置域和应用程序的方法:blog.branch.io/…

N
Nathan de Vries

我认为这样做的侵入性最小的方式如下:

检查用户代理是否是 iPhone/iPod Touch 的 检查 appInstalled cookie 如果 cookie 存在并设置为 true,则将 window.location 设置为 your-uri://(或执行重定向服务器端)如果cookie 不存在,打开“您知道您的站点名称有一个 iPhone 应用程序吗?”带有“是的,我已经得到它”、“不,但我很想尝试一下”和“别管我”按钮的模态。 “是”按钮将 cookie 设置为 true 并重定向到 your-uri:// “否”按钮重定向到“http://itunes.com/apps/yourappname”,这将在设备上打开 App Store “别管我”按钮将 cookie 设置为 false 并关闭模式

我玩过但发现有点笨拙的另一个选项是在 Javascript 中执行以下操作:

setTimeout(function() {
  window.location = "http://itunes.com/apps/yourappname";
}, 25);

// If "custom-uri://" is registered the app will launch immediately and your
// timer won't fire. If it's not set, you'll get an ugly "Cannot Open Page"
// dialogue prior to the App Store application launching
window.location = "custom-uri://";

很好的解决方案。如果您的后备是另一个应用程序,它将立即加载而不显示错误。因此,不要退回到 itunes.com... 使用 itms://phobos.apple.com/... 来避免弹出错误!
问题:当 window.location="custom-uri:// 成功时,回退超时没有被终止。当用户从您的应用程序返回浏览器时,计时器仍然存在,并将启动应用程序商店链接。这是糟糕的用户体验。
似乎 Cookie 在 #ios6 中被沙盒化,因此您无法从另一个应用程序访问由应用程序设置的 cookie。 (例如 App WebUI 和 Safari Mobile)
当用户返回浏览器时,任何人都找到了防止原始超时触发的方法? (JoJo提到的upvoted问题)
要解决 JoJo 提到的问题,只有在用户单击链接后没有经过“很多时间”的情况下才在超时中运行代码。请参阅此解决方案:stackoverflow.com/a/14751543/533420
C
Community

只要您的后备是另一个应用链接,就可以在 JavaScript 中执行此操作。在 Nathan's suggestion 上构建:

<html>
  <head>
    <meta name="viewport" content="width=device-width" />
  </head>
  <body>

    <h2><a id="applink1" href="fb://profile/116201417">open facebook with fallback to appstore</a></h2>
    <h2><a id="applink2" href="unknown://nowhere">open unknown with fallback to appstore</a></h2>
    <p><i>Only works on iPhone!</i></p>    

  <script type="text/javascript">

// To avoid the "protocol not supported" alert, fail must open another app.
var appstorefail = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";

function applink(fail){
    return function(){
        var clickedAt = +new Date;
        // During tests on 3g/3gs this timeout fires immediately if less than 500ms.
        setTimeout(function(){
            // To avoid failing on return to MobileSafari, ensure freshness!
            if (+new Date - clickedAt < 2000){
                window.location = fail;
            }
        }, 500);    
    };
}

document.getElementById("applink1").onclick = applink(appstorefail);
document.getElementById("applink2").onclick = applink(appstorefail);

</script>
</body>
</html>

Check out a live demo here


同意 Lee 的观点,这似乎是一个更直接的解决方案 - 尽管如果应用程序不存在并且它重定向到应用程序商店,我仍然会从 safari 收到错误消息。
我在 Android 和 iOS 上都使用了这个解决方案。我发现如果我将超时值从 500 更改为 100,我不会在 iOS 中看到“无法打开页面”弹出对话框。我还发现Android的超时时间需要为50
使用“itms-apps:”而不是“itms:”可节省 1 次重定向并直接在 appstore 中打开应用页面。
@Rossini 这是通过设置 intent filter for your action that responds to a particular host 内置在 Android 中的
如果未安装应用程序并且在重定向到应用程序商店之前,有谁知道如何避免来自 safari 的“无法打开页面”错误消息?
o
ohho

对于 iOS 6 设备,有一个选项:Promoting Apps with Smart App Banners


遗憾的是,智能应用横幅仅在 Mobile Safari 中支持,而不在 UIWebviewComponent 中支持。因此,例如,如果您的网站显示在 Twitter 客户端中,则不会显示。
c
cnotethegr8

我发现所选答案适用于浏览器应用程序,但在实现 UIWebView 的非浏览器应用程序中运行的代码存在问题。

对我来说,问题是 Twitter 应用程序上的用户会单击一个链接,该链接将通过 Twitter 应用程序中的 UIWebView 将他们带到我的网站。然后,当他们从我的站点单击按钮时,Twitter 会尝试花哨,并且仅在站点可访问时才完成 window.location。所以会发生 UIAlertView 弹出说您确定要继续,然后立即重定向到 App Store 没有第二个弹出窗口。

我的解决方案涉及 iframe。这样可以避免显示 UIAlertView,从而提供简单而优雅的用户体验。

jQuery

var redirect = function (location) {
    $('body').append($('<iframe></iframe>').attr('src', location).css({
        width: 1,
        height: 1,
        position: 'absolute',
        top: 0,
        left: 0
    }));
};

setTimeout(function () {
    redirect('http://itunes.apple.com/app/id');
}, 25);

redirect('custom-uri://');

Javascript

var redirect = function (location) {
    var iframe = document.createElement('iframe');
    iframe.setAttribute('src', location);
    iframe.setAttribute('width', '1px');
    iframe.setAttribute('height', '1px');
    iframe.setAttribute('position', 'absolute');
    iframe.setAttribute('top', '0');
    iframe.setAttribute('left', '0');
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
};

setTimeout(function () {
    redirect('http://itunes.apple.com/app/id');
}, 25);

redirect('custom-uri://');

编辑:

将绝对位置添加到 iframe,以便在插入时在页面底部没有随机的空白位。

另外需要注意的是,我还没有发现 Android 需要这种方法。使用 window.location.href 应该可以正常工作。


有用!!谢谢,我终于找到了适用于所有浏览器的解决方案。
如果找到,这是最好的解决方案。谢谢你。不再有错误弹出窗口。
@Tim 如果您希望在单击链接时触发此代码,则将此代码包装在单击链接后调用的函数中。
@cnotethegr8 我把它放在一个函数中并重定向到自定义 url 效果很好,但回退到 iTunes 没有。这是我的code。我错过了什么?
有没有其他人在 iOS 9 中遇到过这个问题。它在 Safari 中不再适用于我。
s
severin

在 iOS9 中,Apple 终于引入了注册您的应用程序以处理某些 http:// URL 的可能性:Universal Links

一个非常粗略的解释它是如何工作的:

您声明有兴趣在您的应用程序中为某些域(网络 URL)打开 http:// URL。

在指定域的服务器上,您必须指出要在哪个应用程序中打开哪些 URL,该应用程序已声明有兴趣从服务器的域中打开 URL。

iOS URL 加载服务会检查所有尝试打开 http:// URL 以进行上述设置,并在安装时自动打开正确的应用程序;无需先通过 Safari ......

这是在 iOS 上进行深度链接的最简洁的方法,不幸的是它只适用于 iOS9 和更新版本......


并且它在浏览器内部不起作用......仅在外部,例如来自图像或注释
B
BFar

再次建立内森和 JB 的答案:

如何在没有额外点击的情况下从 url 启动应用程序 如果您更喜欢不包括单击链接的中间步骤的解决方案,可以使用以下方法。使用这个 javascript,我能够从 Django/Python 返回一个 Httpresponse 对象,如果它已安装,则该对象成功启动应用程序,或者在超时的情况下启动应用程序商店。注意我还需要将超时时间从 500 调整到 100,以便在 iPhone 4S 上工作。测试和调整以使其适合您的情况。

<html>
<head>
   <meta name="viewport" content="width=device-width" />
</head>
<body>

<script type="text/javascript">

// To avoid the "protocol not supported" alert, fail must open another app.
var appstorefail = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";

var loadedAt = +new Date;
setTimeout(
  function(){
    if (+new Date - loadedAt < 2000){
      window.location = appstorefail;
    }
  }
,100);

function LaunchApp(){
  window.open("unknown://nowhere","_self");
};
LaunchApp()
</script>
</body>
</html>

z
zyanlu
window.location = appurl;// fb://method/call..
!window.document.webkitHidden && setTimeout(function () {
    setTimeout(function () {
    window.location = weburl; // http://itunes.apple.com/..
    }, 100);
}, 600);

document.webkitHidden 是检测你的应用程序是否已经被调用并且当前 safari 标签是否进入后台,此代码来自 www.baidu.com


我测试了这个解决方案,发现虽然它可以正确传递事件,但会暂时出现“Safari 无法打开此页面,因为地址无效”错误对话框。 (它会在几分之一秒后自动关闭)。
使用 iframe 加载 appurlweburl 可以解决您的问题
@zyanlu:我尝试过使用 iFrame。 bt 仍然 safari 显示相同的错误。
q
q0rban

如果您在网页上添加 iframe 并将 src 设置为您的应用程序的自定义方案,iOS 将自动重定向到应用程序中的该位置。如果未安装该应用程序,则不会发生任何事情。这使您可以在已安装的应用程序中进行深度链接,如果未安装,则可以重定向到应用程序商店。

例如,如果您安装了 twitter 应用程序,并导航到包含以下标记的网页,您将立即被定向到该应用程序。

<!DOCTYPE html>
<html>
    <head>
    <title>iOS Automatic Deep Linking</title>
    </head>
    <body>
        <iframe src="twitter://" width="0" height="0"></iframe>
        <p>Website content.</p>
    </body>
</html>

这是一个更详尽的示例,如果未安装应用程序,它将重定向到应用程序商店:

<!DOCTYPE html>
<html>
    <head>
    <title>iOS Automatic Deep Linking</title>
    <script src='//code.jquery.com/jquery-1.11.2.min.js'></script>
    <script src='//mobileesp.googlecode.com/svn/JavaScript/mdetect.js'></script>
    <script>
      (function ($, MobileEsp) {
        // On document ready, redirect to the App on the App store.
        $(function () {
          if (typeof MobileEsp.DetectIos !== 'undefined' && MobileEsp.DetectIos()) {
            // Add an iframe to twitter://, and then an iframe for the app store
            // link. If the first fails to redirect to the Twitter app, the
            // second will redirect to the app on the App Store. We use jQuery
            // to add this after the document is fully loaded, so if the user
            // comes back to the browser, they see the content they expect.
            $('body').append('<iframe class="twitter-detect" src="twitter://" />')
              .append('<iframe class="twitter-detect" src="itms-apps://itunes.com/apps/twitter" />');
          }
        });
      })(jQuery, MobileEsp);
    </script>
    <style type="text/css">
      .twitter-detect {
        display: none;
      }
    </style>
    </head>
    <body>
    <p>Website content.</p>
    </body>
</html>

您的第一个示例的问题是,如果您返回 Mobile Safari,即使已启动 Twitter,它也会显示“未安装 Twitter 应用程序”。与第二个示例相同,显示“网站内容”。如果安装了应用程序,则需要有执行不同操作的代码(加载另一个 URL,或显示两条消息之一)。
是的,@mahboudz,如果您阅读了文本,这只是一个简单的示例,表明可以自动重定向到应用程序。
然后,我会跟进一个更全面的示例,该示例将显示实际的网站内容。我可以删除“未安装 Twitter 应用程序”文本,如果这样更清楚的话。
ios 6 仍然显示弹出那个页面因为无效的url而无法打开
@AndreiShender,以下是撰写本文时的 iOS 使用统计数据:monosnap.com/image/8eXUcpEUi8fm94DiMZIdiIp4xUNaln.png iOS 8:72%,iOS 7:25%,早期版本:3%
D
Dane Macaulay

这是一个解决方案。

使用模糊和焦点设置布尔值

//see if our window is active
window.isActive = true;
$(window).focus(function() { this.isActive = true; });
$(window).blur(function() { this.isActive = false; });

将链接与调用类似这样的 jquery 单击处理程序绑定。

function startMyApp(){
  document.location = 'fb://';

  setTimeout( function(){
    if (window.isActive) {
        document.location = 'http://facebook.com';
    }
  }, 1000);
}

如果应用程序打开,我们将失去对窗口的关注并且计时器结束。否则我们什么也得不到,我们加载通常的 facebook url。


非常感谢您的建议。我面临的问题是“启动外部应用程序”对话框似乎足以让模糊使标志失效。即使未安装应用程序也会发生这种情况(例如,当单击旨在在桌面上启动 iPhone 应用程序的链接时)。想法?
F
Fraser Speirs

据我所知,您无法让整个操作系统理解 http:+ 域 URL。您只能注册新方案(我在我的应用程序中使用 x-darkslide:)。如果安装了该应用程序,Mobile Safari 将正确启动该应用程序。

但是,您必须使用“还在这里?单击此链接从 iTunes 下载应用程序”来处理未安装应用程序的情况。在您的网页中。


这不再是正确的:使用 iOS9 和最新的 Android 版本,您可以注册您的应用程序以侦听某些 http URL
f
frankodwyer

检查 User-Agent,如果它是 Mobile Safari,打开 myprotocol:// URL 以(尝试)打开 iPhone 应用程序并让它打开 Mobile iTunes 以下载应用程序,以防尝试失败

这对我来说听起来是一种合理的方法,但我认为您无法将它作为第二个手段来打开移动 iTunes。我认为你必须选择一个或另一个 - 重定向到你的应用程序或 itunes。

即,如果您重定向到 myprotocol://,并且应用程序不在手机上,您将没有第二次机会重定向到 itunes。

您也许可以首先重定向到(iphone 优化的)登录页面,并让用户选择点击进入您的应用程序,或者如果他们没有应用程序,则可以通过 iTunes 获取该应用程序?但是,您将依赖用户在那里做正确的事情。 (编辑:虽然你可以设置一个 cookie,所以这只是第一次?)


那是错误的。如果显示无法打开页面(App not installed)的错误,仍然执行JS。这就是为什么您可以重定向到另一个后备解决方案的原因。
T
Titignes

在寻求解决弹出问题的过程中,我发现苹果有办法解决这个问题。

事实上,当您点击 this link 时,如果您安装了应用程序,它会被重新路由到它;否则,您将被重定向到网页,而不会弹出任何窗口。


我深入研究了该链接的工作原理,我能想到的最好的结果是它根本不是 JavaScript 解决方案。 Apple 似乎为他们的应用注册了一个特殊的 URL 处理程序,它不需要自定义协议,而是在 URL 字符串上进行一些匹配。您发送的链接会立即通过 303 重定向到 here。如果您通过电子邮件将该链接发送给自己,您会发现点击它会直接打开 AppStore 应用(如果已安装)
很有意思。你是对的:如果我点击它,它会打开 AppStore 应用程序(如果已安装)。但是如果你在“圣日”之前删除一些参数,它会在 Safari 中出现。苹果可以注册特殊的 URL 方案...
@Titignes,您能否详细说明这种打开应用程序或网页的方式。构建这样一个 url 的模式是什么?
D
Den Kison

也可以通过 document.hidden 属性检查标签活动

可能的解决方案

document.location = 'app://deep-link';

setInterval( function(){
  if (!document.hidden) {
    document.location = 'https://app.store.link';
  }
}, 1000);

但似乎这在 Safari 中不起作用