Updated: Better iPad Handling, Improved Windows Mobile Detection

I just uploaded the code bases for the following issues:

  • In the DetectIphone() method, added a call so that the Apple iPad does not provide a false positive. (We also do this so that iPod Touches don’t report themselves as iPhones.)
  • In the QuickMobile() method, added a call for the iPad so that iPads do not report as TRUE. Why do this? Instead, if you want to provide a special UI design for iPads (such as special layouts or to remove Flash), then make an explicit call using the new DetectIpad() method. Otherwise, let the iPad visitors get your regular web site rather than a site optimized for a small mobile phone’s screen.
  • In the DetectWindowsMobile() method, added some new calls. One call checks for “ppc”, which is an indicator of Windows Mobile Professional (aka PocketPC) and is often the only clue for many such devices. The other call looks for both “HTC” and “Windows”. Many HTC Windows Mobile Professional devices try to pass for regular PCs, so this seems to be the best technique for sniffing (most of) them out.

I’ve already uploaded the code to the Google code repository. Please let me know if you spot any bugs!

UPDATED (June 6, 2010): Thanks to Tony, we discovered that the initial enhancement to detect PocketPC (“PPC”) in the DetectWindowsMobile() method also accidentally caught Macintosh PowerPC browsers. So I did a quick update today based on the suggestions which ought to hopefully disambiguate the desktop Macs.

Owner of the MobileESP project. For the latest source code downloads, visit: https://github.com/ahand/mobileesp

Posted in Code Updates
11 comments on “Updated: Better iPad Handling, Improved Windows Mobile Detection
  1. Joshua says:

    Thanks for the code – there are extra spaces after closing php bracket that break headers. I’d recommend just removing closing tag altogether – kinda standard practice now.

  2. Tony says:

    I implemented this for a customer using Blackbaud Net Community and it’s working great with one exception, it seems there are a few people using desktop macs and Safari that are being detected as mobile devices, it’s odd because I’m using a iMac and tested with Safari but wasn’t detected as a mobile device. I use OnDetectMobileQuick and am wondering if anyone else has had problems with desktop Macs with Safari
    OnDetectMobileQuick += new DetectMobileQuickHandler(goHomeDisplay_OnDetectMobileQuick);
    if they use firefox or IE they are not detected as mobile only with Safari

  3. Tony says:

    Here is some information that might be useful:

    It might have something to do with PowerPC vs. Intel Macs :

    This would have incorrectly redirected (PPC Mac and Safari?):
    User Agent: MOZILLA/5.0 (MACINTOSH; U; PPC MAC OS X 10_4_11; EN) APPLEWEBKIT/531.22.7 (KHTML, LIKE GECKO) VERSION/4.0.5 SAFARI/531.22.7
    this.DetectMobileLong()
    this.DetectMobileQuick()
    this.DetectSmartphone()
    this.DetectTierRichCss()
    this.DetectWebkit()
    this.DetectWindowsMobile()

    This one would have correctly stayed (PPC Mac and IE?):
    User Agent: MOZILLA/4.0 (COMPATIBLE; MSIE 5.23; MAC_POWERPC)

    This is my iMac Safari which correctly would not redirect
    User Agent: MOZILLA/5.0 (WINDOWS; U; WINDOWS NT 5.1; EN-US) APPLEWEBKIT/533.4 (KHTML, LIKE GECKO) CHROME/5.0.375.55 SAFARI/533.4
    this.DetectWebkit()

    This is my iPhone which would correctly redirect
    User Agent: MOZILLA/5.0 (IPHONE; U; CPU IPHONE OS 3_1_3 LIKE MAC OS X; EN-US) APPLEWEBKIT/528.18 (KHTML, LIKE GECKO) VERSION/4.0 MOBILE/7E18 SAFARI/528.16
    this.DetectIphone()
    this.DetectIphoneOrIpod()
    this.DetectMobileLong()
    this.DetectMobileQuick()
    this.DetectSmartphone()
    this.DetectTierIphone()
    this.DetectWebkit()

  4. Tony says:

    Sorry for the multiple posts but here’s what I did to fix this:

    Under
    //Initialize some initial smartphone private string private stringiables.
    I added:
    private string deviceMacPpc = “macintosh”.ToUpper(); //Power PC Mac OS X

    Then in DetectWindowsMobile() I changed
    useragent.IndexOf(devicePpc) != -1 ||
    to
    ((useragent.IndexOf(devicePpc) != -1)&&(useragent.IndexOf(deviceMacPpc) == -1)) ||

  5. Anthony Hand says:

    Thanks for the suggestion, Tony. I added that little patch for the Macintosh PowerPC (also “PPC”) disambiguation to all 4 platforms (PHP, JavaScript, Java, .NET). I used a slightly different coding technique, but it worked in my informal testing, at least on JavaScript and PHP.

    Can you download the update and put it through the test?

  6. Anthony Hand says:

    Thanks for the note, Josh. I’ve since removed the closing PHP bracket from the PHP code for just that reason.

  7. Underwood says:

    I have a question about how to use this.

    This seems to work on both my laptop and iPhone, but I’m not confident with php classes (I have no idea!):

    DetectTierIphone() == 1){ echo ‘iPhone’; }
    else if ( $a->DetectTierRichCss() == 1 || $a->DetectTierOtherPhones() == 1 ){ echo ‘Other mobile device.’; }
    else { echo ‘Pooter!’; }
    ?>

    Is it that simple? Is there something important I’m missing?

    Will gladly donate if this code works for me 🙂

  8. Anthony Hand says:

    @Underwood – Yes, that’s pretty much it! What I generally recommend is something like this framework:

    – If iPhone Tier, show fancy content for iPhone/Android/PalmOS devices.

    – Else if other mobile, show plain vanilla mobile content. So for this step, you can either use “DetectMobileQuick()” because you’ve already filtered out the other devices, or the detect for TierRichCSS and Other Phones methods in your example. Same difference.

    – Else, it’s probably a desktop browser so you can let them see the regular desktop content.

  9. Underwood says:

    Thanks Anthony

    (Check that your paypal button is working on the Donate page. It seems to take me to a generic paypal page…)

  10. Anthony Hand says:

    Thanks for the heads up, Underwood. I just fixed the Donate button on the Donations page.

  11. Because the DetectMobile() methods only return true IF a mobile device is detected, then by letting all else pass — which could be desktops or other types of devices you don’t care about or don’t know about — you’re making the “Else” statement get the default behavior. Simple logic.

Leave a Reply

Your email address will not be published. Required fields are marked *