从 Safari 启动应用程序或应用程序商店?

发布于 2024-11-28 08:21:23 字数 87 浏览 3 评论 0原文

我已经知道如何从 safari 启动应用程序,但是否可以在启动前检查应用程序是否已安装?如果 iPhone 上当前未安装该应用程序,我正在考虑启动应用程序商店。

I already know how to launch an app from safari, but is it possible to check if the app is installed before launching? I'm thinking to launch the app store if the app isn't currently installed on the iPhone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别忘他 2024-12-05 08:21:23

无法从网页检查应用程序是否已安装。您可以在其他应用程序中执行此操作,方法是检查您的 url 方案是否可以使用 UIApplication 的 -canOpenURL: 方法打开,但没有与此等效的 javascript。

但是,您可以使用以下解决方法:

<script language="javascript">
    function open_appstore() {
        window.location='http://itunes.com/';
    }

    function try_to_open_app() {
        setTimeout('open_appstore()', 300);
    }
</script>

<a onClick="javascript:try_to_open_app();" href="yourappurl:">App name</a>

此代码将在链接上设置超时,如果超时结束,该链接将调用 open_appstore 函数。由于您的链接指向应用程序的自定义 URL,Safari 将尝试打开该链接,如果可以,它将打开应用程序并停止计时器,因此 AppStore 链接将不会打开。

如果应用程序链接无法打开,当计时器耗尽时,它将显示一个错误弹出窗口,指出无法打开页面(无法摆脱该错误),但它会立即转到 AppStore 并消除该错误。


iOS 9 添加了一个非常好的功能,可以让您的应用打开 http/s url:通用链接


iOS 10 当您点击链接并安装应用程序时,会出现一个弹出窗口,显示“在 [应用程序名称] 中打开”。如果用户在给定的超时内没有点击“打开”,则此解决方案将使用后备。
由于 300 毫秒太短,无法点击任何内容,此解决方案在 iOS 10 上总是失败

It's not possible to check if app is installed from a web page. You could do it inside an other app by checking if your url scheme can be opened using UIApplication's -canOpenURL: method, but there is no javascript equivalent to this.

However, you can use the following workaround:

<script language="javascript">
    function open_appstore() {
        window.location='http://itunes.com/';
    }

    function try_to_open_app() {
        setTimeout('open_appstore()', 300);
    }
</script>

<a onClick="javascript:try_to_open_app();" href="yourappurl:">App name</a>

This code will set a timeout on the link that will call the open_appstore function if this timeout ends. Since your link is pointed at the app's custom url, Safari will try to open that link and if it can, it will open the app and stop the timer, so AppStore link will not be opened.

If the app link can't be opened, when timer runs out it will display an error popup saying it can't open the page (can't get rid of that), but it will immediately go to AppStore and dismiss that error.


iOS 9 adds a really nice feature that lets your app open a http/s url: Universal Links


In iOS 10 there is a popup saying "Open in [App Name]" when you tap the link and the app is installed. If the user does not tap on "Open" in the given timeout, this solution will use the fallback.
As 300ms is too short to tap anything, this solution always fails on iOS 10.

烟织青萝梦 2024-12-05 08:21:23

这对我来说有类似的情况:其中我想打开 gmaps 应用程序(如果支持) - 否则直接转到 gmap 网站。

function mapLink(addy) {     
  addy = encodeURIComponent(addy);

  var fallback = 'http://maps.google.com/?q=' + addy
  , link = 'comgooglemaps://?q=' + addy;

  try {
    document.location = link;
  } catch(err) {
    document.location = fallback;
  }
}

似乎非常适合我的用例。

更新:
如果你想在回退时创建一个新窗口,这仍然允许弹出 ios 错误消息。要解决它,请尝试这个。

  try {
    document.location = link;
  } catch(err) {
    window.location.reload(true);
    window.open(fallback, '_blank');
  }

This worked for me with a similar situation: wherein I wanted to open gmaps app if it was supported - otherwise go to gmap site directly.

function mapLink(addy) {     
  addy = encodeURIComponent(addy);

  var fallback = 'http://maps.google.com/?q=' + addy
  , link = 'comgooglemaps://?q=' + addy;

  try {
    document.location = link;
  } catch(err) {
    document.location = fallback;
  }
}

Seems to work pretty well for my use case.

Update:
If you want to do a new window on fallback, this still allowed the ios error message to pop up. To get around it try this.

  try {
    document.location = link;
  } catch(err) {
    window.location.reload(true);
    window.open(fallback, '_blank');
  }
梦中的蝴蝶 2024-12-05 08:21:23

Apple 的解决方案:

来自 Apple 文档

https://developer.apple.com/library/ios/documentation/AppleApplications/ Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

如果应用程序已安装在用户的设备上,横幅会智能地更改其操作,点击横幅即可打开应用程序。如果用户的设备上没有您的应用程序,点击横幅将把他带到应用程序商店中的应用程序条目。当他返回您的网站时,横幅中会出现一个进度条,指示下载需要多长时间才能完成。应用程序下载完成后,“查看”按钮将更改为“打开”按钮,点击横幅将打开应用程序,同时保留用户在您网站上的上下文。

智能应用横幅会自动确定用户设备是否支持该应用。如果加载横幅的设备不支持您的应用,或者您的应用在用户所在位置不可用,则横幅将不会显示。

要将智能应用横幅添加到我们的网页,请在您希望显示横幅的每个页面的头部添加以下元标记:

注意:我们还可以传递应用程序参数:如 myName 等,

检查是否在 此页面

更新:

1.一旦您关闭了显示的横幅,即使您在 html 中有该元标记,该横幅也不会再次显示。

2.要重置该设置,请启动设置应用程序,然后导航至“常规”>“重新发送”>“重置所有设置”

The Solution from Apple:

From Apple Documentation

https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

If the app is already installed on a user's device, the banner intelligently changes its action, and tapping the banner will simply open the app. If the user doesn’t have your app on his device, tapping on the banner will take him to the app’s entry in the App Store. When he returns to your website, a progress bar appears in the banner, indicating how much longer the download will take to complete. When the app finishes downloading, the View button changes to an Open button, and tapping the banner will open the app while preserving the user’s context from your website.

Smart App Banners automatically determine whether the app is supported on the user’s device. If the device loading the banner does not support your app, or if your app is not available in the user's location, the banner will not display.

To add a Smart App Banner to our webpage, include the following meta tag in the head of each page where you’d like the banner to appear:

NOTE: We can also pass the app-argument: like myName,etc.,

Check that Providing Navigational Context to Your App Header in this Page

Updates:

1. Once you have closed the banner that showing up, then that will not be displayed again even though you had that meta tag in our html.

2. To reset that launch the settings App then navigate to General>Resent>Reset all settings

公布 2024-12-05 08:21:23

您可以简单地读取方法-(BOOL)openURL:(NSURL)url*的返回值,如果为NO,则表示目标应用程序未安装。以下代码片段给出了使用 navigon url 方案的示例:

NSString *stringURL = @"navigon://coordinate/NaviCard/19.084443/47.573305";
NSURL *url = [NSURL URLWithString:stringURL];
if([[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"Well done!");
} else {
    stringURL = @"https://itunes.apple.com/it/app/id320279293?mt=8";
    url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];
}

感谢 zszen 的更正。

You can simply read the return value from the method -(BOOL)openURL:(NSURL)url*, if it's NO, it means that the target application isn't installed. Following code snipped gives an example using the navigon url scheme:

NSString *stringURL = @"navigon://coordinate/NaviCard/19.084443/47.573305";
NSURL *url = [NSURL URLWithString:stringURL];
if([[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"Well done!");
} else {
    stringURL = @"https://itunes.apple.com/it/app/id320279293?mt=8";
    url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];
}

Thanks to zszen for the correction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文