redirect到应用程序,如果安装,否则到应用程序

我知道可以通过注册一个自定义scheme(例如://)直接链接到iOS的应用程序,也可以通过iTunes链接到应用程序中的应用程序。

在很多情况下,理想的stream程是提供一个链接, 如果已安装, redirect到应用程序, 否则将链接redirect到应用程序。 这是可能的,如果是这样,怎么样?

为了清楚起见 ,这个场景是我打开一个链接(http)从我的iphone上的电子邮件邀请我join一个应用程序组。 如果用户在该设备上安装了应用程序,则应该打开该应用程序,否则http链接应该redirect到iTunes。

没有办法检查这个。 但是,有一个很好的解决方法。

这个想法基本上是这样的:

  1. 当您第一次打开您的应用程序时,您将从您的应用程序中打开移动Safari浏览器到您服务器上的预定义URL
  2. 在这个URL上,你设置了一个cookie,就像appInstalled给用户移动Safari一样
  3. 然后用注册scheme将用户带回到您的应用程序(与FB与SSO一样)
  4. 您所有的电子邮件链接指向您的网站,但在网站上,您检查浏览器是否是移动Safari和如果appInstalled cookie存在
  5. 如果浏览器不是移动版Safari浏览器,或者找不到cookie,您将redirect到AppStore,或者保留在您的网页中。
  6. 如果#4的条件为真,则将用户redirect到注册scheme的应用程序
  7. 如果该应用程序已被用户删除,所以自定义urlscheme失败,您有一个故障安全redirect到应用程序

最后两个步骤在这个SOpost中解释

我认为更简单的答案将是在您的服务器上使用以下JavaScript设置一个页面:

(function() { var app = { launchApp: function() { window.location.replace("myapp://"); this.timer = setTimeout(this.openWebApp, 1000); }, openWebApp: function() { window.location.replace("http://itunesstorelink/"); } }; app.launchApp(); })(); 

这主要是试图redirect到你的应用程序,并设置超时redirect到应用程序商店,如果失败。

你甚至可以使代码更加智能一些,并检查用户代理,看看他们是一个iOS用户,一个Android用户,或一个Web用户,然后适当的redirect。

如果您的电子邮件中包含一个网页,并且该网页包含一个将src设置为您的应用程序的自定义scheme的iframe ,则iOS将自动redirect到该应用程序中的该位置。 如果应用程序没有安装,什么都不会发生。 这可以让你深入链接到应用程序,如果它安装,或redirect到App Store,如果没有安装。

例如,如果您安装了twitter应用程序,并导航到包含以下标记的网页,则会立即转到该应用程序。 如果您没有安装Twitter应用程序,则会看到“Twitter应用程序未安装”文本。

 <!DOCTYPE html> <html> <head> <title>iOS Automatic Deep Linking</title> </head> <body> <iframe src="twitter://" width="0" height="0"></iframe> <p>The Twitter App is not installed</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> 

是的,它很容易。 这就要求你打开的应用程序在plist中声明一个urlscheme:

 //if you can open your app if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yourapp://"]]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"yourapp://"]]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"ituneappstorelink"]]; }