如何从Web应用程序打开本机iOS应用程序 [英] How to open a native iOS app from a web app

查看:103
本文介绍了如何从Web应用程序打开本机iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要我的应用程序具有正确运行的URL方案,我想从主屏幕上存储的Web应用程序启动,而普通的JavaScript重定向方法似乎不起作用。

Summary I have an app with a correctly functioning URL scheme that I'd like to launch from a web app stored on the home screen, and the normal JavaScript redirect methods don't seem to work.

详细信息我正在尝试创建一个iOS网络应用程序,以全屏模式从主页上保存的链接打开屏幕。 Web应用程序需要打开特定的本机应用程序。我已经为本机应用程序注册了url方案,并验证它是否正常工作 - 例如,我可以通过直接在我的Safari地址栏中键入方案来打开本机应用程序。我也可以使用 UIApplication + openURL:方法从其他应用程序打开它。我还想用可以添加到主屏幕的本地Web应用程序中的某些参数打开它。

Details I'm trying to create an iOS web app, to be opened in full-screen mode from a link saved on the Home Screen. The web app needs to open a specific native app. I have already registered the url scheme for the native app, and verified that it works correctly - I can open the native app by typing the scheme directly into my Safari address bar, for instance. I can also open it from other applications using the +openURL: method of UIApplication. I would like to also open it with certain arguments from a native web app that can be added to the home screen.

我要做的是使用JavaScript之类的所以在原生应用程序中:

What I'm trying to do is use JavaScript like so inside the native app:

window.location = "myapp://myparam";

在网络应用程序中使用此代码时,我收到一条警告说:

When using this code inside the web app I get an alert saying:


无法打开myWebAppName - myWebAppName无法打开。
错误是此网址无法显示。。

"Cannot Open myWebAppName - myWebAppName could not be opened. The error was "This URL can't be shown".".

在Safari中执行时,这个javascript工作正常。我使用 window.location.replace(myapp:// myparam)获得相同的结果。

This same javascript when executed within Safari works correctly. I get the same result using window.location.replace("myapp://myparam").

网络应用程序的HTML是:

The html for the web app is:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <meta name="generator" content="TextMate http://macromates.com/">
    <meta name="author" content="Carl Veazey">
    <!-- Date: 2012-04-19 -->
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta names="apple-mobile-web-app-status-bar-style" content="black-translucent" />
</head>
<body>
    <script type="text/javascript" charset="utf-8">
        if (window.navigator.userAgent.indexOf('iPhone') != -1) {
            if (window.navigator.standalone == true) {
                window.location = "myapp://myparam";
            } else {
                document.write("please save this to your home screen");
        };} else {
                alert("Not iPhone!");
                document.location.href = 'please-open-from-an-iphone.html';
        };
    </script>
</body>
</html>

我在这里做错了什么?我对javascript和移动网络缺乏经验,所以我怀疑我只是遗漏了一些明显的东西。

What am I doing wrong here? I'm pretty inexperienced with javascript and mobile web so I suspect I'm just missing something obvious.

推荐答案

如果您的代码有效,它在移动野生动物园中,但不是来自iOS桌面上的书签。从来没有尝试过这种方式,但这就是问题所在。如果我只是将你的JS设置为

Your code works if its in mobile safari but NOT if its from a bookmark on the iOS desktop. Never tried it that way before, but thats the issue. If i just set your JS to

<script type="text/javascript" charset="utf-8"> 
window.location = "myapp://myparam";
</script>

它适用于浏览器,但加入书签后会失败。它可能需要做一些关于如何加载URL的书签,因为没有铬?我的猜测是苹果不希望预订标记页面访问本地应用程序。此外,我注意到,如果我为本地托管的页面添加书签,这可以在移动版Safari中使用,我无法通过书签加载它。它真的很奇怪....

It works in browser, but when bookmarked it fails. It might have to do something with how the url is loaded when its bookmarked since there is no chrome? My guess is that apple doesn't want booked mark pages to access local apps. Also, I've noticed that if I bookmark a locally hosted page, that works in mobile safari, I can't get it to load via bookmark. Its really odd....

我给你的最好的建议就是让它

Best recommendation I have for you is to make it

<meta name="apple-mobile-web-app-capable" />

而不是

<meta name="apple-mobile-web-app-capable" content="yes" />

这种方式它将出现在主屏幕上,但不幸的是会加载chrome。这是我能想到的唯一解决方案。

This way it will be on the home screen, but will unfortunately load with the chrome. Thats the only solution I can think of.

这篇关于如何从Web应用程序打开本机iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆