每当手机上安装应用程序时,我想让 iOS 使用我的应用程序从我的域(例如 http://martijnthe.nl)打开 URL,如果没有,则使用 Mobile Safari。
我读到可以为此创建一个唯一的协议后缀并将其注册到 Info.plist 中,但如果未安装该应用程序,Mobile Safari 会出错。
什么是解决方法?
一个想法:
1) 使用在任何桌面浏览器中打开的 http:// URL,并通过浏览器呈现服务
2) 检查 User-Agent,如果它是 Mobile Safari,打开 myprotocol:// URL 以(尝试)打开 iPhone 应用程序并让它打开 Mobile iTunes 以下载应用程序,以防尝试失败
不确定这是否可行...建议?谢谢!
我认为这样做的侵入性最小的方式如下:
检查用户代理是否是 iPhone/iPod Touch 的 检查 appInstalled cookie 如果 cookie 存在并设置为 true,则将 window.location 设置为 your-uri://(或执行重定向服务器端)如果cookie 不存在,打开“您知道您的站点名称有一个 iPhone 应用程序吗?”带有“是的,我已经得到它”、“不,但我很想尝试一下”和“别管我”按钮的模态。 “是”按钮将 cookie 设置为 true 并重定向到 your-uri:// “否”按钮重定向到“http://itunes.com/apps/yourappname”,这将在设备上打开 App Store “别管我”按钮将 cookie 设置为 false 并关闭模式
我玩过但发现有点笨拙的另一个选项是在 Javascript 中执行以下操作:
setTimeout(function() {
window.location = "http://itunes.com/apps/yourappname";
}, 25);
// If "custom-uri://" is registered the app will launch immediately and your
// timer won't fire. If it's not set, you'll get an ugly "Cannot Open Page"
// dialogue prior to the App Store application launching
window.location = "custom-uri://";
只要您的后备是另一个应用链接,就可以在 JavaScript 中执行此操作。在 Nathan's suggestion 上构建:
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<h2><a id="applink1" href="fb://profile/116201417">open facebook with fallback to appstore</a></h2>
<h2><a id="applink2" href="unknown://nowhere">open unknown with fallback to appstore</a></h2>
<p><i>Only works on iPhone!</i></p>
<script type="text/javascript">
// To avoid the "protocol not supported" alert, fail must open another app.
var appstorefail = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";
function applink(fail){
return function(){
var clickedAt = +new Date;
// During tests on 3g/3gs this timeout fires immediately if less than 500ms.
setTimeout(function(){
// To avoid failing on return to MobileSafari, ensure freshness!
if (+new Date - clickedAt < 2000){
window.location = fail;
}
}, 500);
};
}
document.getElementById("applink1").onclick = applink(appstorefail);
document.getElementById("applink2").onclick = applink(appstorefail);
</script>
</body>
</html>
对于 iOS 6 设备,有一个选项:Promoting Apps with Smart App Banners
我发现所选答案适用于浏览器应用程序,但在实现 UIWebView
的非浏览器应用程序中运行的代码存在问题。
对我来说,问题是 Twitter 应用程序上的用户会单击一个链接,该链接将通过 Twitter 应用程序中的 UIWebView
将他们带到我的网站。然后,当他们从我的站点单击按钮时,Twitter 会尝试花哨,并且仅在站点可访问时才完成 window.location
。所以会发生 UIAlertView
弹出说您确定要继续,然后立即重定向到 App Store 没有第二个弹出窗口。
我的解决方案涉及 iframe。这样可以避免显示 UIAlertView
,从而提供简单而优雅的用户体验。
jQuery
var redirect = function (location) {
$('body').append($('<iframe></iframe>').attr('src', location).css({
width: 1,
height: 1,
position: 'absolute',
top: 0,
left: 0
}));
};
setTimeout(function () {
redirect('http://itunes.apple.com/app/id');
}, 25);
redirect('custom-uri://');
Javascript
var redirect = function (location) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', location);
iframe.setAttribute('width', '1px');
iframe.setAttribute('height', '1px');
iframe.setAttribute('position', 'absolute');
iframe.setAttribute('top', '0');
iframe.setAttribute('left', '0');
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
};
setTimeout(function () {
redirect('http://itunes.apple.com/app/id');
}, 25);
redirect('custom-uri://');
编辑:
将绝对位置添加到 iframe,以便在插入时在页面底部没有随机的空白位。
另外需要注意的是,我还没有发现 Android 需要这种方法。使用 window.location.href
应该可以正常工作。
在 iOS9 中,Apple 终于引入了注册您的应用程序以处理某些 http://
URL 的可能性:Universal Links。
一个非常粗略的解释它是如何工作的:
您声明有兴趣在您的应用程序中为某些域(网络 URL)打开 http:// URL。
在指定域的服务器上,您必须指出要在哪个应用程序中打开哪些 URL,该应用程序已声明有兴趣从服务器的域中打开 URL。
iOS URL 加载服务会检查所有尝试打开 http:// URL 以进行上述设置,并在安装时自动打开正确的应用程序;无需先通过 Safari ......
这是在 iOS 上进行深度链接的最简洁的方法,不幸的是它只适用于 iOS9 和更新版本......
再次建立内森和 JB 的答案:
如何在没有额外点击的情况下从 url 启动应用程序 如果您更喜欢不包括单击链接的中间步骤的解决方案,可以使用以下方法。使用这个 javascript,我能够从 Django/Python 返回一个 Httpresponse 对象,如果它已安装,则该对象成功启动应用程序,或者在超时的情况下启动应用程序商店。注意我还需要将超时时间从 500 调整到 100,以便在 iPhone 4S 上工作。测试和调整以使其适合您的情况。
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<script type="text/javascript">
// To avoid the "protocol not supported" alert, fail must open another app.
var appstorefail = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";
var loadedAt = +new Date;
setTimeout(
function(){
if (+new Date - loadedAt < 2000){
window.location = appstorefail;
}
}
,100);
function LaunchApp(){
window.open("unknown://nowhere","_self");
};
LaunchApp()
</script>
</body>
</html>
window.location = appurl;// fb://method/call..
!window.document.webkitHidden && setTimeout(function () {
setTimeout(function () {
window.location = weburl; // http://itunes.apple.com/..
}, 100);
}, 600);
document.webkitHidden
是检测你的应用程序是否已经被调用并且当前 safari 标签是否进入后台,此代码来自 www.baidu.com
appurl
和 weburl
可以解决您的问题
如果您在网页上添加 iframe
并将 src
设置为您的应用程序的自定义方案,iOS 将自动重定向到应用程序中的该位置。如果未安装该应用程序,则不会发生任何事情。这使您可以在已安装的应用程序中进行深度链接,如果未安装,则可以重定向到应用程序商店。
例如,如果您安装了 twitter 应用程序,并导航到包含以下标记的网页,您将立即被定向到该应用程序。
<!DOCTYPE html>
<html>
<head>
<title>iOS Automatic Deep Linking</title>
</head>
<body>
<iframe src="twitter://" width="0" height="0"></iframe>
<p>Website content.</p>
</body>
</html>
这是一个更详尽的示例,如果未安装应用程序,它将重定向到应用程序商店:
<!DOCTYPE html>
<html>
<head>
<title>iOS Automatic Deep Linking</title>
<script src='//code.jquery.com/jquery-1.11.2.min.js'></script>
<script src='//mobileesp.googlecode.com/svn/JavaScript/mdetect.js'></script>
<script>
(function ($, MobileEsp) {
// On document ready, redirect to the App on the App store.
$(function () {
if (typeof MobileEsp.DetectIos !== 'undefined' && MobileEsp.DetectIos()) {
// Add an iframe to twitter://, and then an iframe for the app store
// link. If the first fails to redirect to the Twitter app, the
// second will redirect to the app on the App Store. We use jQuery
// to add this after the document is fully loaded, so if the user
// comes back to the browser, they see the content they expect.
$('body').append('<iframe class="twitter-detect" src="twitter://" />')
.append('<iframe class="twitter-detect" src="itms-apps://itunes.com/apps/twitter" />');
}
});
})(jQuery, MobileEsp);
</script>
<style type="text/css">
.twitter-detect {
display: none;
}
</style>
</head>
<body>
<p>Website content.</p>
</body>
</html>
这是一个解决方案。
使用模糊和焦点设置布尔值
//see if our window is active
window.isActive = true;
$(window).focus(function() { this.isActive = true; });
$(window).blur(function() { this.isActive = false; });
将链接与调用类似这样的 jquery 单击处理程序绑定。
function startMyApp(){
document.location = 'fb://';
setTimeout( function(){
if (window.isActive) {
document.location = 'http://facebook.com';
}
}, 1000);
}
如果应用程序打开,我们将失去对窗口的关注并且计时器结束。否则我们什么也得不到,我们加载通常的 facebook url。
据我所知,您无法让整个操作系统理解 http:
+ 域 URL。您只能注册新方案(我在我的应用程序中使用 x-darkslide:
)。如果安装了该应用程序,Mobile Safari 将正确启动该应用程序。
但是,您必须使用“还在这里?单击此链接从 iTunes 下载应用程序”来处理未安装应用程序的情况。在您的网页中。
http
URL
检查 User-Agent,如果它是 Mobile Safari,打开 myprotocol:// URL 以(尝试)打开 iPhone 应用程序并让它打开 Mobile iTunes 以下载应用程序,以防尝试失败
这对我来说听起来是一种合理的方法,但我认为您无法将它作为第二个手段来打开移动 iTunes。我认为你必须选择一个或另一个 - 重定向到你的应用程序或 itunes。
即,如果您重定向到 myprotocol://,并且应用程序不在手机上,您将没有第二次机会重定向到 itunes。
您也许可以首先重定向到(iphone 优化的)登录页面,并让用户选择点击进入您的应用程序,或者如果他们没有应用程序,则可以通过 iTunes 获取该应用程序?但是,您将依赖用户在那里做正确的事情。 (编辑:虽然你可以设置一个 cookie,所以这只是第一次?)
在寻求解决弹出问题的过程中,我发现苹果有办法解决这个问题。
事实上,当您点击 this link 时,如果您安装了应用程序,它会被重新路由到它;否则,您将被重定向到网页,而不会弹出任何窗口。
也可以通过 document.hidden
属性检查标签活动
可能的解决方案
document.location = 'app://deep-link';
setInterval( function(){
if (!document.hidden) {
document.location = 'https://app.store.link';
}
}, 1000);
但似乎这在 Safari 中不起作用
不定期副业成功案例分享
window.location="custom-uri://
成功时,回退超时没有被终止。当用户从您的应用程序返回浏览器时,计时器仍然存在,并将启动应用程序商店链接。这是糟糕的用户体验。