我发现所选答案适用于浏览器应用程序,但我的代码在实现的非浏览器应用程序中无法正常工作UIWebView。
对我来说,问题是Twitter应用程序上的用户将单击一个链接,该链接将使他们通过UIWebViewTwitter应用程序中的进入我的网站。然后,当他们单击我的网站上的按钮时,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://');
Java脚本
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应该可以正常工作。