ios 从 Safari 启动应用程序或应用程序商店?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964515/
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
Launching app OR app store from Safari?
提问by ninjaneer
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.
我已经知道如何从 safari 启动应用程序,但是否可以在启动前检查应用程序是否已安装?如果 iPhone 上当前未安装该应用程序,我正在考虑启动该应用程序商店。
回答by Filip Radelic
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.
无法检查是否从网页安装了应用程序。您可以通过检查是否可以使用 UIApplication 的 -canOpenURL: 方法打开您的 url 方案来在其他应用程序中执行此操作,但是没有与此等效的 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. 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.
如果超时结束,此代码将在将调用 open_appstore 函数的链接上设置超时。由于您的链接指向应用程序的自定义 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 9adds a really nice feature that lets your app open a http/s url: Universal Links
iOS 9添加了一个非常好的功能,让你的应用程序打开一个 http/s url:通用链接
In iOS 10there 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.
在iOS 10 中,当您点击链接并安装应用程序时,会出现一个弹出窗口,提示“在 [应用程序名称] 中打开”。如果用户没有点击给定中的“打开”,则timeout
此解决方案将使用回退。由于 300 毫秒太短,无法点击任何内容,因此此解决方案在 iOS 10 上总是失败。
回答by William Hall
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. To get around it try this.
更新:如果您想在回退时创建一个新窗口,这仍然允许弹出 ios 错误消息。为了解决它,试试这个。
try {
document.location = link;
} catch(err) {
window.location.reload(true);
window.open(fallback, '_blank');
}
回答by Vijay-Apple-Dev.blogspot.com
The Solution from Apple:
苹果的解决方案:
From Apple Documentation
来自苹果文档
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.,
注意:我们也可以传递 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. 要重置启动设置应用程序,然后导航到常规>重新发送>重置所有设置
回答by Zeb
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:
您可以简单地从方法-(BOOL)openURL:(NSURL)url* 中读取返回值,如果为 NO,则表示未安装目标应用程序。以下代码截图给出了一个使用 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.
感谢 zszen 的更正。