我希望iOS能够在我的应用程序安装在手机上时使用我的应用程序打开我的域中的URL(例如http://martijnthe.nl),如果不是,则使用Mobile Safari.
我读过可以为此创建一个唯一的协议后缀并将其注册到Info.plist中,但是如果未安装该应用程序,Mobile Safari将会出错.
什么是解决方法?
一个想法:
1)使用在任何桌面浏览器中打开的http:// URL,并通过浏览器呈现服务
2)检查用户代理,如果是移动Safari,打开myprotocol:// URL(尝试)打开iPhone应用程序并打开移动iTunes以下载应用程序,以防尝试失败
不确定这是否有效...建议?谢谢!
Nat*_*ies 240
我认为这样做最不具侵入性的方法如下:
appInstalledcookiewindow.location为your-uri://(或执行重定向服务器端)your-uri://我玩过的另一个选项但发现有点笨拙的是在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://";
Run Code Online (Sandbox Code Playgroud)
jb.*_*jb. 95
只要您的后备是另一个applink,就可以在JavaScript中执行此操作.建立在内森的建议之上:
<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>
Run Code Online (Sandbox Code Playgroud)
ohh*_*hho 26
对于iOS 6设备,有一个选项:使用智能应用程序横幅推广应用程序
cno*_*gr8 23
我发现所选答案适用于浏览器应用程序,但我遇到了在非浏览器应用程序中实现的代码问题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://');
Run Code Online (Sandbox Code Playgroud)
使用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://');
Run Code Online (Sandbox Code Playgroud)
编辑:
向iframe添加绝对位置,因此在插入时,页面底部没有随机的空白位.
另外值得注意的是,我还没有发现Android需要这种方法.使用window.location.href应该工作正常.
sev*_*rin 20
在iOS9中,Apple最终推出了注册您的应用以处理某些http://URL 的可能性:Universal Links.
它是如何工作的非常粗略的解释:
http://在您的应用中打开某些域(网址)的网址.http://设置URL的尝试,并在安装时自动打开正确的应用程序; 没有首先通过Safari ...这是在iOS上进行深层链接的最简洁方法,不幸的是它仅适用于iOS9及更新版本......
在Nathan和JB的答案上再次建立:
如何从没有额外点击的网址启动应用程序 如果您更喜欢不包含单击链接的临时步骤的解决方案,可以使用以下内容.使用这个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>
Run Code Online (Sandbox Code Playgroud)
小智 9
window.location = appurl;// fb://method/call..
!window.document.webkitHidden && setTimeout(function () {
setTimeout(function () {
window.location = weburl; // http://itunes.apple.com/..
}, 100);
}, 600);
Run Code Online (Sandbox Code Playgroud)
document.webkitHidden 是检测您的应用程序是否已被调用以及当前的safari选项卡是否转到后台,此代码来自www.baidu.com
If you add an iframe on your web page with the src set to custom scheme for your App, iOS will automatically redirect to that location in the App. If the app is not installed, nothing will happen. This allows you to deep link into the App if it is installed, or redirect to the App Store if it is not installed.
For example, if you have the twitter app installed, and navigate to a webpage containing the following markup, you would be immediately directed to the app.
<!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>
Run Code Online (Sandbox Code Playgroud)
Here is a more thorough example that redirects to the App store if the App is not installed:
<!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>
Run Code Online (Sandbox Code Playgroud)