首页 文章

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

提问于
浏览
218

我希望iOS能够在我的应用程序安装在手机上时使用我的应用程序打开我的域名(例如http://martijnthe.nl),如果不是,则使用Mobile Safari .

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

什么是解决方法?

一个想法:

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

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

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

12 回答

  • 235

    我发现所选答案适用于浏览器应用程序,但我遇到了在非浏览器应用程序中实现 UIWebView 的代码问题 .

    对我来说问题是Twitter应用程序上的用户会点击一个链接,通过Twitter应用程序中的 UIWebView 将它们带到我的网站 . 然后,当他们从我的网站点击一个按钮时,Twitter试图变得花哨,只有在网站可以访问时才完成 window.location . 所以会发生什么是弹出声明你确定要继续然后立即重定向到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 应该可以正常工作 .

  • 94

    这是一个解决方案 .

    使用模糊和焦点设置布尔坐标

    //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网址 .

  • 0
    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

  • 9

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

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

    • 您声明有兴趣在您的应用中打开某些域(网址)的 http:// 个网址 .

    • 在指定域的服务器上,您必须指明要在哪个应用程序中打开哪些URL,这些应用程序已声明对从服务器域打开URL感兴趣 .

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

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

  • 2

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

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

    这是一个更全面的示例,如果未安装App,则重定向到App Store:

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

    在Nathan和JB的答案上再次 Build :

    How To Launch App From url w/o Extra Click 如果您更喜欢不包含单击链接的临时步骤的解决方案,则可以使用以下内容 . 使用这个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>
    
  • 23

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

    • 检查用户代理是否是iPhone / iPod Touch的用户代理

    • 检查 appInstalled cookie

    • 如果cookie存在且设置为true,请将 window.location 设置为 your-uri:// (或执行重定向服务器端)

    • 如果cookie不存在,请使用"Yep, I've already got it","Nope, but I'd love to try it"和"Leave me alone"按钮打开"Did you know Your Site Name has an iPhone application?"模式 .

    • "Yep"按钮将cookie设置为true并重定向到 your-uri://

    • "Nope"按钮重定向到“http://itunes.com/apps/yourappname”,这将打开设备上的App Store

    • "Leave me alone"按钮将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://";
    
  • 0

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

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

    即如果您重定向到myprotocol://,并且该应用程序不在手机上,您将无法再次有机会重定向到iTunes .

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

  • 9

    在寻求解决弹出问题时,我发现Apple已经解决了这个问题 .

    实际上,当您单击this link时,如果您安装了该应用程序,则会将其重新路由到该应用程序;否则,您将被重定向到网页,没有任何弹出窗口 .

  • 5

    只要您的后备是另一个applink,就可以在JavaScript中执行此操作 . Build 在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 .

  • 26

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

  • 2

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

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

相关问题