Dashcode如何区分iPad和iPhone浏览器?

3
我正在尝试找出如何使Dashcode Web应用程序区分iPhone浏览器和iPad浏览器。一个可行的例子是Apple iPad用户指南。iPad将显示一个流畅的dashcode构建的界面,而iPhone则会被重定向到网页。
我在Howto force DashCode question中找到了一些帮助。
我正在编辑redirector.js文件。以下内容强制iPad使用由Dashcode构建的Safari布局,而不是Mobile Safari,这正是我想要的。当从iPhone浏览时,它会返回一个文件未找到错误。
// redirect to the more appropriate product
if (DCProductURLs["mobileweb"] && DCshowiPhone) {
    // Changed case so that the Safari layout is displayed on iPad
    // window.location.href = DCProductURLs["mobileweb"];
    window.location.href = DCProductURLs["desktop"];
}

感谢任何建议。

3个回答

4

测试 window.navigator.userAgent。在 iPad 上会包括 iPad,在 iPod touch 上会包括 iPod。

var oniPad = /iPad/.test(window.navigator.userAgent);

嘿,谢谢你。最终我使用了一些不同的东西,但是你的答案让我找到了正确的方向。我的问题之一是混淆了我的Objective-C和Javascript。唉! - DenVog

0

我修改了我的redirector.js文件,现在它正好做你想要的事情,也许这不是最好的方法,但它能够工作,以下是我的代码,希望对你有用:

var DCProductURLs = {
    "mobileweb": "../mobile", 
    "desktop": "../safari"
};

var DCshowiPhone = RegExp(" AppleWebKit/").test(navigator.userAgent) && RegExp(" Mobile/").test(navigator.userAgent);

var DCqs = window.location.search.substring(1);
if ((DCshowiPhone && DCqs.length > 0)||screen.width>1000) {
    var components = DCqs.split("&");
    for (var f = 0; f < components.length; f++) {
        if (components[f] == "p=desktop") {
            DCshowiPhone = false;
            break;
        }
    }
}

// redirect to the more appropriate product
//var device= 
if (DCProductURLs["mobileweb"] && DCshowiPhone && screen.width<=960) {
    window.location.href = DCProductURLs["mobileweb"];
}

0

最终我采用了一些基于ScottRockers博客中的这篇文章的代码。感谢ughoavgfhw让我找到了正确的方向。

if ((navigator.userAgent.indexOf('iPad') != -1)) {
    window.location.href = DCProductURLs["desktop"];
}

if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1)) {
    window.location.href = DCProductURLs["mobileweb"];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接