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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1108693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:08:27  来源:igfitidea点击:

Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?

iphoneiosurl

提问by Martijn Thé

I'd like to have iOS to open URLs from my domain (e.g. http://martijnthe.nl) with my app whenever the app is installed on the phone, and with Mobile Safari in case it is not.

我想让 iOS在手机上安装应用程序时使用我的应用程序打开来自我的域(例如http://martijnthe.nl)的URL ,如果不是,则使用 Mobile Safari。

I read it is possible to create a unique protocol suffix for this and register it in the Info.plist, but Mobile Safari will give an error in case the app is not installed.

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

What would be a workaround?

什么是解决方法?

One idea:

一个想法:

1) Use http:// URLs that open in any desktop browser and render the service through the browser

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

2) Check the User-Agent and in case it's Mobile Safari, open a myprotocol:// URL to (attempt) to open the iPhone app and have it open Mobile iTunes to the download of the app in case the attempt fails

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

Not sure if this will work... suggestions? Thanks!

不确定这是否有效...建议?谢谢!

回答by Nathan de Vries

I think the least intrusive way of doing this is as follows:

我认为这样做的侵入性最少的方法如下:

  1. Check if the user-agent is that of an iPhone/iPod Touch
  2. Check for an appInstalledcookie
  3. If the cookie exists and is set to true, set window.locationto your-uri://(or do the redirect server side)
  4. If the cookie doesn't exist, open a "Did you know Your Site Name has an iPhone application?" modal with a "Yep, I've already got it", "Nope, but I'd love to try it", and "Leave me alone" button.
    1. The "Yep" button sets the cookie to true and redirects to your-uri://
    2. The "Nope" button redirects to "http://itunes.com/apps/yourappname" which will open the App Store on the device
    3. The "Leave me alone" button sets the cookie to false and closes the modal
  1. 检查用户代理是否是 iPhone/iPod Touch 的用户代理
  2. 检查appInstalledcookie
  3. 如果cookie存在并且设置为true,设置window.locationyour-uri://(或者做重定向服务器端)
  4. 如果 cookie 不存在,请打开“您知道您的站点名称有 iPhone 应用程序吗?” 带有“是的,我已经拿到了”、“不,但我很想试试”和“别管我”按钮的模态。
    1. “是”按钮将 cookie 设置为 true 并重定向到 your-uri://
    2. “Nope”按钮重定向到“ http://itunes.com/apps/yourappname”,这将在设备上打开 App Store
    3. “让我一个人呆着”按钮将 cookie 设置为 false 并关闭模态

The other option I've played with but found a little clunky was to do the following in Javascript:

我玩过但发现有点笨拙的另一个选项是在 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://";

回答by jb.

It's quite possible to do this in JavaScript as long as your fallback is another applink. Building on Nathan's suggestion:

只要您的回退是另一个应用程序链接,就很有可能在 JavaScript 中执行此操作。基于Nathan 的建议

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

在此处查看现场演示

回答by ohho

For iOS 6 devices, there is an option: Promoting Apps with Smart App Banners

对于 iOS 6 设备,有一个选项:使用智能应用横幅推广应用

回答by cnotethegr8

I found that the selected answer works for the browser apps but I was having issues with the code working in non browser apps that implement a UIWebView.

我发现所选答案适用于浏览器应用程序,但我在实现UIWebView.

The problem for me was a user on the Twitter app would click a link that would take them to my site through a UIWebViewin the Twitter app. Then when they clicked a button from my site Twitter tries to be fancy and only complete the window.locationif the site is reachable. So what happens is a UIAlertViewpops up saying are you sure you want to continue and then immediately redirects to the App Store without a second popup.

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

My solution involves iframes. This avoids the UIAlertViewbeing presented allowing for a simple and elegant user experience.

我的解决方案涉及 iframe。这避免了UIAlertView被呈现,允许简单而优雅的用户体验。

jQuery

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

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://');

EDIT:

编辑:

Add position absolute to the iframe so when inserted there isn't a random bit of whitespace at the bottom of the page.

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

Also it's important to note that I have not found a need for this approach with Android. Using window.location.hrefshould work fine.

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

回答by severin

In iOS9 Apple finally introduced the possibility to register your app to handle certain http://URLs: Universal Links.

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

A very rough explanation of how it works:

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

  • You declare interest in opening http://URLs for certain domains (web urls) in your app.
  • On the server of the specified domains you have to indicate which URLs to open in which app that has declared interest in opening URLs from the server's domain.
  • The iOS URL loading service checks all attempts to open http://URLs for a setup as explained above and opens the correct app automatically if installed; without going through Safari first...
  • 您声明有兴趣http://在您的应用中打开某些域的网址(网址)。
  • 在指定域的服务器上,您必须指明要在哪个应用程序中打开哪些 URL,该应用程序已声明有兴趣从服务器域中打开 URL。
  • iOS URL 加载服务会检查所有打开http://URL 的尝试,如上文所述,并在安装后自动打开正确的应用程序;无需先通过 Safari...

This is the cleanest way to do deep linking on iOS, unfortunately it works only in iOS9 and newer...

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

回答by zyanlu

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

document.webkitHiddenis to detect if your app is already invoked and current safari tab to going to the background, this code is from www.baidu.com

document.webkitHidden是检测你的app是否已经被调用,当前safari tab进入后台,这段代码来自www.baidu.com

回答by BFar

BUILDING Again on Nathan and JB's Answer:

再次构建 Nathan 和 JB 的回答:

How To Launch App From url w/o Extra ClickIf you prefer a solution that does not include the interim step of clicking a link, the following can be used. With this javascript, I was able to return a Httpresponse object from Django/Python that successfully launches an app if it is installed or alternatively launches the app store in the case of a time out. Note I also needed to adjust the timeout period from 500 to 100 in order for this to work on an iPhone 4S. Test and tweak to get it right for your situation.

如何从没有额外点击的 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>

回答by q0rban

If you add an iframeon your web page with the srcset to custom scheme for your App, iOS will automatically redirect to that location in the App. If the app is not installed, nothing will happen. This allows you to deep link into the App if it is installed, or redirect to the App Store if it is not installed.

如果您iframe在网页上src为应用添加自定义方案,iOS 将自动重定向到应用中的该位置。如果未安装该应用程序,则不会发生任何事情。如果已安装,这允许您深入链接到应用程序,或者如果未安装,则重定向到 App Store。

For example, if you have the twitter app installed, and navigate to a webpage containing the following markup, you would be immediately directed to the app.

例如,如果您安装了 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>

Here is a more thorough example that redirects to the App store if the App is not installed:

这是一个更彻底的示例,如果未安装应用程序,则重定向到应用程序商店:

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

回答by Dane Macaulay

Heres a solution.

这是一个解决方案。

Setup a boolean sitiation using blur and focus

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

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

Bind your link with a jquery click handler that calls something like this.

将您的链接与调用类似内容的 jquery 单击处理程序绑定。

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

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

if the app opens, we'll lose focus on the window and the timer ends. otherwise we get nothing and we load the usual facebook url.

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

回答by Fraser Speirs

You can't, as far as I know, make the entire OS understand an http:+domain URL. You can only register new schemes (I use x-darkslide:in my app). If the app is installed, Mobile Safari will launch the app correctly.

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

However, you would have to handle the case where the app isn't installed with a "Still here? Click this link to download the app from iTunes." in your web page.

但是,您必须处理应用程序未安装并显示“仍在此处?单击此链接以从 iTunes 下载应用程序”的情况。在您的网页中。