简体   繁体   English

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

[英]Launching app OR app store from Safari?

I already know how to launch an app from safari, but is it possible to check if the app is installed before launching? 我已经知道如何从safari启动应用程序,但是可以在启动之前检查应用程序是否已安装? I'm thinking to launch the app store if the app isn't currently installed on the iPhone. 如果iPhone上当前没有安装该应用,我正在考虑启动应用商店。

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. 您可以通过检查您的url方案是否可以使用UIApplication的-canOpenURL:方法打开,在其他应用程序中执行此操作,但没有与此等效的javascript。

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

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. 如果无法打开应用程序链接,当计时器用完时,它将显示一个错误弹出窗口,说它无法打开页面(无法摆脱它),但它会立即转到AppStore并解除该错误。


iOS 9 adds a really nice feature that lets your app open a http/s url: Universal Links iOS 9增加了一个非常好的功能,让你的应用程序打开一个http / s网址: 通用链接


In iOS 10 there is a popup saying " Open in [App Name] " when you tap the link and the app is installed. iOS 10中 ,当您点击链接并安装了应用程序时,会弹出一个“ 在[应用程序名称]中打开 ”的弹出窗口。 If the user does not tap on " Open " in the given timeout , this solution will use the fallback. 如果用户未在给定timeout点击“ 打开 ”,则此解决方案将使用回退。 As 300ms is too short to tap anything, this solution always fails on iOS 10 . 由于300毫秒太短,无法点击任何东西, 这个解决方案总是在iOS 10上失败

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. 这对我来说也有类似的情况:如果支持的话我想打开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;
  }
}

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. 更新:如果你想在后备上做一个新窗口,这仍然允许弹出ios错误消息。 To get around it try this. 为了解决它试试这个。

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

The Solution from Apple: Apple的解决方案:

From Apple Documentation 来自Apple文档

https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html 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. 如果用户没有在他的设备上安装您的应用程序,点击横幅将使他进入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., 注意:我们也可以传递app-argument:如myName等,

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. 1.一旦你关闭了显示的横幅,即使你在我们的html中有了这个元标记,也不会再显示。

2. To reset that launch the settings App then navigate to General>Resent>Reset all settings 2.要重置启动设置应用程序,然后导航到常规>重新发送>重置所有设置

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. 您只需从方法- (BOOL)openURL:(NSURL )url *中读取返回值,如果为NO,则表示未安装目标应用程序。 Following code snipped gives an example using the navigon url scheme: 以下代码剪切提供了一个使用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];
}

Thanks to zszen for the correction. 感谢zszen的纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM