Detect iPhone 3G or 3GS (and iPod touch 2G vs iPod touch 3G) in JavaScript

后端 未结 3 1662
醉梦人生
醉梦人生 2021-02-07 20:16

Does anyone know a way to detect the older set of devices:

  • iPod touch 1G
  • iPhone 2G
  • iPhone 3G
  • iPod touch 2G

From the newer

相关标签:
3条回答
  • 2021-02-07 20:55

    Little Benchmark that detects older models. The Benchmark takes 5-30ms so no real problem.

    function runBenchmark(){
        var time,
        init = new Date(),
        iterations = 5000;
        while (iterations--) {
            Math.sqrt(iterations*Math.random());
        }
        time = new Date - init;
        return time;
    }
    
    function detectIphoneSpeed () {
        if (runBenchmark()<15) {
            return "3gs";
        } else {
            return "3g";
        }
    }
    var iphoneSpeed = detectIphoneSpeed();
    
    0 讨论(0)
  • 2021-02-07 20:56

    Have you looked into the UserAgent string?

    For example, my iPhone 4 returns:

    Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7

    The Mobile/8A306 is the firmware version (just google for 8a306 iphone

    I believe the older versions of the phones cannot run the new versions of the OS. So you might be able to find a set of firmware codes that will only show up on the older versions of the phones. This will at least allow you to detect some of the differences.

    The WURFL has some good references and user agent strings and how to detect the device and capabilities based on it. There is a browseable list of them, and they might be able to detect the devices correctly. I don't have access to the older hardware, so I can't confirm it works all the time. You can explore what your device shows up as using: http://www.tera-wurfl.com/explore/ And to look at the various UserAgent strings given back.

    0 讨论(0)
  • 2021-02-07 21:03

    If number crunching would be a discriminator... use it! Just benchmark a small loop (call it BogoMips) and within some 0.5 seconds you know what you are up to.

    The hardware and software are defined, the load is pretty much defined (well.. iOS 4 'multitasking'?), so I think it can be very accurate.

    edit I only read your remarks on performance testing now; maybe you meant this exactly, maybe you meant to measure page render time. My suggestion is to have a page with only a piece of javascript which then stores the result as a cookie and redirects. Pretty much all circumstances are known then.

    0 讨论(0)
提交回复
热议问题