是否有可能注册一个基于http +域名的iPhone应用程序的URL计划,如YouTube和地图?

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

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

什么是解决方法?

一个想法:

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

2)检查用户代理,如果它是移动Safari,打开一个myprotocol:// URL到(尝试)打开iPhone应用程序,并打开手机iTunes的应用程序下载的情况下,尝试失败

不知道这是否会工作…build议? 谢谢!

我认为这样做的最less干扰方式如下:

  1. 检查用户代理是否是iPhone / iPod Touch的用户代理
  2. 检查一个appInstalled cookie
  3. 如果cookie存在并设置为true,则将window.location设置为your-uri:// (或者执行redirect服务器端)
  4. 如果Cookie不存在,请打开“您知道您的网站名称是否有iPhone应用程序?” 模式与“是的,我已经得到它”,“不,但我想尝试”,并“离开我一个人”button。
    1. “Yep”button将cookie设置为true并redirect到your-uri://
    2. “Nope”buttonredirect到“ http://itunes.com/apps/yourappname ”,这将打开设备上的App Store
    3. “离开我”button将cookie设置为false并closures模式

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

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

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

看看这里的现场演示 。

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

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

对我来说,问题是Twitter应用程序上的用户会点击一个链接,通过Twitter应用程序中的UIWebView将它们带到我的网站。 然后,当他们点击我的网站上的一个button时,Twitter试图看中,如果该网站是可及的只完成window.location 。 那么会发生什么是一个UIAlertViewpopup,你确定要继续,然后立即redirect到App Store没有第二个popup窗口。

我的解决scheme涉及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应该可以正常工作。

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

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

  • 您声明有兴趣在您的应用中打开特定网域(url)的http://url。
  • 在指定域的服务器上,您必须指出要在哪个应用程序中声明要打开来自服务器域的URL的URL。
  • iOS URL加载服务将检查所有尝试打开http:// URL的设置,如上所述,并自动打开正确的应用程序,如果安装; 没有通过Safari首先…

这是在iOS上进行深度链接的最干净的方式,不幸的是它只能在iOS9和更新版本中使用…

再次在Nathan和JB的答案:

如何从无需额外点击的URL启动应用程序如果您更喜欢的解决scheme不包括单击链接的临时步骤,则可以使用以下内容。 有了这个JavaScript,我能够从Django / Python返回一个Httpresponse对象,如果它已经安装,或者在超时的情况下启动应用程序商店,它将成功启动应用程序。 注意我还需要将超时时间从500秒调整到100秒,以便在iPhone 4S上运行。 testing和调整,以适应您的情况。

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

如果您的网页上添加了一个iframesrc设置为您的应用程序的自定义scheme,iOS将自动redirect到应用程序中的该位置。 如果应用程序没有安装,什么都不会发生。 这可以让你深入链接到应用程序,如果它安装,或redirect到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> 

下面是一个更彻底的例子,如果应用程序没有安装,redirect到App Store:

 <!DOCTYPE html> <html> <head> <title>iOS Automatic Deep Linking</title> <script src='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> 

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

但是,您将不得不处理应用程序未安装“仍在此处?单击此链接从iTunes下载应用程序”的情况。 在你的网页。

下面是一个解决scheme。

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

 //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); } 

如果应用程序打开,我们将失去焦点在窗口和计时器结束。 否则我们得不到任何东西,我们加载通常的Facebookurl。

检查用户代理,如果它是移动Safari,打开一个myprotocol:// URL到(尝试)打开iPhone应用程序,并打开手机iTunes的应用程序下载的情况下,尝试失败

这听起来对我来说是一个合理的方法,但我不认为你会得到它作为第二个手段打开移动iTunes。 我认为你必须select一个或另一个 – redirect到你的应用程序或iTunes。

即,如果您redirect到myprotocol://,并且该应用程序不在电话上,则您将不会再获得redirect到iTunes的机会。

你也许可以首先redirect到一个(iPhone优化的)着陆页面,并让用户select点击到你的应用程序,或者如果没有它的应用程序获取应用程序的iTunes? 但是,您将依靠用户在那里做正确的事情。 (编辑:虽然你可以设置一个cookie,这是只有第一次的东西?)

为了解决popup问题,我发现苹果解决了这个问题。

事实上,当你点击这个链接时 ,如果你安装了应用程序,它将被重新路由到它; 否则,您将被redirect到网页,而不会popup任何内容。