ios 如果已安装,则重定向到应用程序,否则重定向到 App Store

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14048159/
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 21:38:12  来源:igfitidea点击:

Redirect to application if installed, otherwise to App Store

iosredirectapp-store

提问by ConfusedNoob

I know it's possible to link directly to an app in iOS by registering a custom scheme (e.g. so://) and it's also possible to link to the app in the appstore via itunes.

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

In many cases, the ideal flow is to provide a link that redirects to the app ifit's installed and to the store if not. Is this possible, and if so, how?

在许多情况下,理想的流程是提供一个链接,如果已安装,重定向到应用程序,如果未安装,重定向到商店。这是可能的,如果是,如何?

Added for clarity, the scenario is I'm opening a link (http) from an e-mail on my iphone that's inviting me to join a group in an application. If the user has the app installed on that device it should open, otherwise the http link should redirect to itunes.

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

采纳答案by Lefteris

There is no way to check for this. However, there is a nice workaround.

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

The idea is basically this:

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

  1. The first time you open your app, you open up mobile safari from within your app to a predefined URL on your server
  2. On that URL you set up a cookie, like appInstalled to the users mobile safari
  3. You then kick the user back to your app with your registered scheme (same as FB does with SSO)
  4. All your email links point to your website, but on the website you check if the browser is mobile Safari and if the appInstalled cookie exists
  5. If either the browser is not mobile Safari or the cookie is not found, you redirect to the AppStore, or stay in your webpage.
  6. If the conditions of #4 are true, you redirect the user to your app with the registered scheme
  7. If the app has been deleted by the user, so the custom url scheme fails, you have a fail-safe redirect to the appstore
  1. 第一次打开您的应用程序时,您从应用程序内打开移动 safari 到您服务器上的预定义 URL
  2. 在那个 URL 上你设置了一个 cookie,比如 appInstalled to the users mobile safari
  3. 然后,您使用您注册的方案将用户踢回您的应用程序(与 FB 对 SSO 的做法相同)
  4. 您所有的电子邮件链接都指向您的网站,但在网站上您检查浏览器是否为移动版 Safari 以及 appInstalled cookie 是否存在
  5. 如果浏览器不是移动版 Safari 或未找到 cookie,您将重定向到 AppStore,或停留在您的网页中。
  6. 如果 #4 的条件为真,您将用户重定向到您的应用程序,并使用已注册的方案
  7. 如果应用已被用户删除,因此自定义 url 方案失败,则您可以安全地重定向到应用商店

The 2 last steps are explained on this SO post

在此 SO 帖子中解释最后两个步骤

回答by BananaNeil

I think the more simple answer would be to set up a page on your server with the following javascript:

我认为更简单的答案是使用以下 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();
})();

This basically attempts to redirect to your app and sets a timeout to redirect to the app store if it fails.

这基本上是尝试重定向到您的应用程序并设置超时以在失败时重定向到应用程序商店。

You could even make the code a little more smart, and check the user agent to see if they are an ios user, an android user, or a webuser, and then redirect them appropriately.

你甚至可以让代码更智能一点,并检查用户代理,看看他们是 ios 用户、android 用户还是 webuser,然后适当地重定向它们。

回答by q0rban

If you have a web page you link to from the email with the web page containing an iframewith the srcset to the 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.

如果你有一个网页,你的电子邮件链接到包含一个网页iframesrc设置,为您的应用程序自定义方案,,iOS将自动重定向到在App该位置。如果未安装该应用程序,则不会发生任何事情。如果已安装,这允许您深入链接到应用程序,或者如果未安装,则重定向到 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. If you did not have the Twitter app installed, you would see the text "The Twitter App is not installed."

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

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

"Smart App Banners" - not sure when they showed up but after finding this post looking for same, then Smart App Banners, this is follow up.

“智能应用横幅” - 不确定他们什么时候出现,但在找到这篇寻找相同的帖子后,然后是智能应用横幅,这是跟进。

Smart App Banners are a single line html meta tag in the header of each page you want to offer your app over the web experience :

智能应用横幅是您希望通过网络体验提供应用的每个页面标题中的单行 html 元标记:

<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL">

which shows that icon at the top of the page and "Open this page in" with either the app or routing to the App Store.

它在页面顶部显示该图标,并使用应用程序或路由到 App Store 来“打开此页面”。

enter image description here

在此处输入图片说明

The metadata for this page on the iPhone looks like this (anonymized of course):

iPhone 上此页面的元数据如下所示(当然是匿名的):

<meta name="apple-itunes-app" content="app-id=605841731, app-argument=lync://confjoin?url=https://meet.rtc.yourcorporatedomain.com/firstName.lastName/conferenceID">

? Apple Developer Documentation - Promoting Apps with Smart App Banners

? Apple 开发者文档 - 使用智能应用横幅推广应用

回答by rooster117

Yeah its pretty easy. This requires the app you want to open to have a url scheme declared in the plist:

是的,这很容易。这需要您要打开的应用程序在 plist 中声明一个 url 方案:

//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"]];
}

回答by aftab muhammed khan

There are few simple steps to achieve this Action

有几个简单的步骤来实现这个动作

Step 1

第1步

Go -> Project (select target) -> info -> URL Types

转到 -> 项目(选择目标) -> 信息 -> URL 类型

enter image description here

在此处输入图片说明

Create URL Scheme in Xcode Like this

像这样在 Xcode 中创建 URL Scheme

enter image description herehere URL Scheme is myApp (it is better to have all character in lowercase).

在此处输入图片说明这里的 URL Scheme 是 myApp(最好让所有字符都小写)。

Step 2

第2步

Setup Delegate if you have plan to receive parameters/Query strings from URL

如果您计划从 URL 接收参数/查询字符串,则设置委托

Here is the code :

这是代码:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

     NSLog(@"APP : simple action %@",url.scheme);

    if ([url.scheme hasPrefix:@"myapp"]) {

        NSLog(@"APP inside simple %@",url.absoluteString);


        NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
                                                    resolvingAgainstBaseURL:NO];
        NSArray *queryItems = urlComponents.queryItems;
        NSString * abc = [self valueForKey:@"abc"
                           fromQueryItems:queryItems];
        NSString * xyz = [self valueForKey:@"xyz"
                           fromQueryItems:queryItems];


        NSLog(@"Sid up = %@", abc);

        NSLog(@"PID up = %@", xyz);

      // you can do anything you want to do here



        return YES;
    }
return NO;
}

End of Xcode side Work.

Xcode 端工作结束。

Step 3

第 3 步

Referring @BananaNeil Code here as i am not Front end guy

在这里引用@BananaNeil 代码,因为我不是前端人员

(function() {
  var app = {
    launchApp: function() {
      window.location.replace("myApp://share?abc=12&xyz=123");
      this.timer = setTimeout(this.openWebApp, 1000);
    },

    openWebApp: function() {
      window.location.replace("http://itunesstorelink/");
    }
  };

  app.launchApp();
})();

Hope it will HELP you all

希望对大家有帮助

回答by Confused Vorlon

There are a bunch of complicated edge cases here, so the easiest solution is to let someone else handle that stuff.

这里有一堆复杂的边缘情况,所以最简单的解决方案是让其他人处理这些事情。

This is what https://branch.io/do. You can use their free plan to achieve exactly what you want, with a handful of bonus features

这就是https://branch.io/所做的。您可以使用他们的免费计划来实现您想要的目标,并提供一些奖励功能

  • statistics
  • you can pass info along with the link, and it will be retrieved even if the user had to do an install first
  • link will work on the desktop (by default, it will text an install link to your mobile)
  • 统计数据
  • 您可以将信息与链接一起传递,即使用户必须先进行安装,它也会被检索
  • 链接将在桌面上工作(默认情况下,它会将安装链接以文本形式发送到您的手机)

I'm not affiliated with Branch.io, but I do use their product.

我不隶属于 Branch.io,但我确实使用他们的产品。

回答by Lata Tiwari

If someone is still stuck in this issue and needs easiest solution, you will love node-deeplink

如果有人仍然卡在这个问题上并且需要最简单的解决方案,你会喜欢node-deeplink

1.) If app is installed: Calling an app through deep linking will always call componentDidMount of root component. So you can attach a listener there. Like:

1.) 如果安装了应用程序:通过深层链接调用应用程序将始终调用根组件的 componentDidMount。所以你可以在那里附加一个监听器。喜欢:

Linking.getInitialURL()
      .then(url => {
        if (url) {
          this.handleOpenURL({ url });
        }
      })
      .catch(console.error);

    Linking.addEventListener('url', this.handleOpenURL);



handleOpenURL(event) {
    if (event) {
      console.log('event = ', event);
      const url = event.url;
      const route = url.replace(/.*?:\/\//g, '');
      console.log('route = ', route);
      if(route.match(/\/([^\/]+)\/?$/)) {
        const id = route.match(/\/([^\/]+)\/?$/)[1];
        const routeName = route.split('/')[0];

        if (routeName === 'privatealbum') {
          Actions.privateAlbum({ albumId: id });
        }
      }
    }
  }

2.) If app is not installed: Just set up a route in your server and node-deeplinkpackage will handle the bridging between web browser to app store when a app is not installed in your mobile.

2.) 如果未安装应用程序:只需在您的服务器中设置一条路由,当您的手机中未安装应用程序时,node-deeplink包将处理网络浏览器与应用程序商店之间的桥接。

By this, both the cases will be handled without any struggle

如此一来,两起案件都将得到妥善处理