如何从Web应用程序打开本机iOS应用程序

Car*_*zey 28 javascript mobile-safari ios

总结我有一个具有正确功能的URL方案的应用程序,我想从主屏幕上存储的Web应用程序启动,而普通的JavaScript重定向方法似乎不起作用.

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

我想要做的是在本机应用程序中使用这样的JavaScript:

window.location = "myapp://myparam";
Run Code Online (Sandbox Code Playgroud)

在Web应用程序中使用此代码时,我会收到一条提示:

"无法打开myWebAppName - 无法打开myWebAppName.错误是"此URL无法显示"."

在Safari中执行时,这个相同的JavaScript工作正常.我得到了相同的结果window.location.replace("myapp://myparam").

Web应用程序的html是:

<!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>
Run Code Online (Sandbox Code Playgroud)

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

AJa*_*Jak 13

如果您的代码位于移动版Safari中,则代码可以正常工作,但如果它来自iOS桌面上的书签,从来没有尝试过,但这就是问题所在.如果我只是将你的JS设置为

<script type="text/javascript" charset="utf-8"> 
window.location = "myapp://myparam";
</script>
Run Code Online (Sandbox Code Playgroud)

它在浏览器中工作,但在加入书签时失败.由于没有镶边,它可能需要对网址加载时如何加载?我的猜测是苹果不希望预订标记页面访问本地应用程序.另外,我注意到如果我将本地托管的页面加入书签,这可以在移动版Safari中使用,我无法通过书签加载它.真奇怪......

我给你的最好的建议是成功

<meta name="apple-mobile-web-app-capable" />
Run Code Online (Sandbox Code Playgroud)

代替

<meta name="apple-mobile-web-app-capable" content="yes" />
Run Code Online (Sandbox Code Playgroud)

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


And*_*sky 8

如果您需要打开iOS应用程序(如果已安装)并且还想保留页面的功能,location.href = 'myapp://?params=...';则无效,因为如果myapp://未注册,则重定向会将用户引导至无法访问的目标.

最安全的赌注似乎是在使用iframe.以下代码将打开iOS应用程序(如果已安装)并且如果不是则不会导致失败(尽管如果未安装应用程序,它可能会警告用户无法访问该页面):

var frame = document.createElement('iframe');
frame.src = 'myapp://?params=...';
frame.style.display = 'none';
document.body.appendChild(frame);

// the following is optional, just to avoid an unnecessary iframe on the page
setTimeout(function() { document.body.removeChild(frame); }, 4);
Run Code Online (Sandbox Code Playgroud)