Detecting Mobile Devices Using ASP.NET and C#
Thanks to the magical powers of the Internet, a kind fan of the PHP scripts for detecting mobile devices ported the code to ASP.NET and C#. So for those folks using ASP.NET, here is a fantastic new option. And of course -- it's free!
For more detailed information about the caveats of attempting to detect mobile devices based on User Agent strings, please see our article on PHP scripting. There are also some useful links to commercial and open source projects for detecting mobile devices in that article.
Quick apologies: I'm not as versed in ASP.NET as JavaScript and PHP, so any errors introduced in the code are mine. Brad, the gentleman who ported the PHP code to ASP.NET, did a great job! Since he sent the code to me, I've made a few minor modifications, including Palm WebOS support, some additional OnDetect events, and some additional commenting. Please accept my apologies in advance for any errors. And if you find any, please let me know!
Summary
As with PHP and JavaScript code, the basic concept is to do the following:
- Create a custom Web Page class which inherits from the mobile-detecting base class, MDetectPage, described below. All of the mobile dection methods and events are built in.
- Detect whether the current web site visitor is using a mobile device.
- If yes, redirect to a mobile-optimized page. (Or similar type of custom action for mobile devices.)
Introducing the "MDetectPage" Class
The ASP.NET class called "MDetectPage" inherits from System.Web.UI.Page and encapsulates all of the logic for detecting mobile devices. The page adds an elegant delegates and events model for detecting mobile devices that is consistent with the ASP.NET programming paradigm.
As a result, the easiest thing to do is to simply sub-class the MDetectPage and listen for the "OnDetectXXX()" event to fire for the device type that you're interested in. For example, if you'd like to send iPhones and Android devices to your special touch-optized mobile site, listen for the OnDetectTierIphone event to fire. Or, if you only have one mobile site that serves pretty much any type of device, listen instead for the OnDetectMobileQuick event.
Note that this code is easy to use and its API is highly modularized so that you can detect broad classes of devices (such as smartphones or WAP/WMP-capable devices) and specific platforms (such as the iPhone/iPod Touch, Android or BlackBerry). Feel free to further optimize the code for your own uses and fill in any of the missing OnDetect events.
Using the "UAgentInfo" Class
First, instantiate the UAgentInfo object, then call one of its functions. The device detection functions return booleans: true or false. It's as simple as that! Here's an example:
|
//************************** // ASP.NET Service Page name: MDetect_Test.aspx // This page inherits from the MDetectPage. // This test page will listen for an iPhone or // iPod Touch device and redirect it to a special page. public partial class MDetect_Test : MDetectPage { protected void Page_Load(object sender, EventArgs e) { //Call this method to triger the detection methods. this.FireEvents(); } protected override void OnInit(EventArgs e) { base.OnInit(e); //Let's listen for an iPhone or iPod Touch OnDetectDetectIPhoneOrIpod += new DetectIPhoneOrIpodHandler( MDetect_Test_OnDetectDetectIPhoneOrIpod); } void MDetect_Test_OnDetectDetectIPhoneOrIpod( object page, MDetectPage.MDetectArgs args) { //Send the browser to a different page. Response.Status = "301 Moved Permanently"; Response.Redirect("http://www.mycompany.com/iphone"); } } |
Let's look at some specific features of the API.
Detect iPhone & iPod Touch
Use the following code to detect whether the device viewing the page is an iPhone and/or iPod Touch. And don't forget: iPod Touches are devices, too!
|
/** * Detects if the current device is an iPhone or iPod Touch. * In most cases, you'll want to use this function. */ public delegate void DetectIPhoneOrIpodHandler( object page, MDetectArgs args); public event DetectIPhoneOrIpodHandler OnDetectDetectIPhoneOrIpod; /** * Detects if the current device is an iPhone. */ public delegate void DetectIphoneHandler(object page, MDetectArgs args); public event DetectIphoneHandler OnDetectIphone; /** * Detects if the current device is an iPod Touch. */ public delegate void DetectIpodHandler(object page, MDetectArgs args); public event DetectIpodHandler OnDetectIpod; |
Detect Android Devices
Besides the iPhone, the only other mobile devices in recent times to receive as much attention, both from geeky fanboys and the general public, have been the new devices running Android. Android is a brand new operating system for mobile phones created by Google. Android is a smartphone-class OS with an advanced browser similar to the iPhone's in capabilities.
|
/** * Detects if the current device is an Android OS-based device. */ public delegate void DetectAndroidHandler( object page, MDetectArgs args); public event DetectAndroidHandler OnDetectAndroid; |
Detect Symbian S60 Smartphones
The most popular smartphone platform in the world is Symbian S60. Used primarily by Nokia and a few other manufacturers, S60 features a very capable browser.
|
/** * Detects if the current browser is the S60 Open Source Browser. */ public delegate void DetectS60OssBrowserHandler( object page, MDetectArgs args); public event DetectS60OssBrowserHandler OnDetectS60OssBrowser; |
Detect Windows Mobile Devices
Devices running Windows Mobile are fairly popular in the U.S., especially among business users. This code detects both the non-touch screen (Standard/Smartphone) and touch screen (Professional/PocketPC) types of devices.
|
/** * Detects if the current browser is a Windows Mobile device. */ public delegate void DetectWindowsMobileHandler( object page, MDetectArgs args); public event DetectWindowsMobileHandler OnDetectWindowsMobile; |
Detect BlackBerry Devices
The most popular smartphone platform in the United States. (Still beats the iPhone in sales!) Unfortunately, the BlackBerry browser isn't great.
|
/** * Detects if the current browser is on a BlackBerry of some sort. */ public delegate void DetectBlackBerryHandler( object page, MDetectArgs args); public event DetectBlackBerryHandler OnDetectBlackBerry; |
Detect PalmOS and WebOS Devices
Last but not least in the smartphone category are devices from Palm. Palm has a new Linux-based operating system for its touchscreen phones launched in mid-2009 call "WebOS". The Pre and the Pixi are the first two WebOS devices. In addition, there is a large installed base of old PalmOS devices.
|
/** * Detects if the current browser is on a newer Palm device * running the new WebOS. */ public delegate void DetectPalmWebOSHandler( object page, MDetectArgs args); public event DetectPalmWebOSHandler OnDetectPalmWebOS; |
Additional API Details
Now that we've seen the general pattern for how to use our MDetectPage class works, let's see a few more types of devices it can detect. In the interest of space, we won't show the code for each. (But you can download it below!)
- Detect any smartphone-class device: OnDetectSmartphone
- Detect any iPhone-tier touchscreen device, including Android and Palm WebOS: OnDetectTierIphone
- Detect any browser based on WebKit: OnDetectWebkit
- Quick check to detect any mobile device. Ought to detect most modern mass market phones as well as smartphones: OnDetectMobileQuick
- This device's browser supports WAP/WML content: OnDetectWapWml
- This device is BREW-powered: OnDetectBrewDevice
- This is a Danger Hiptop: OnDetectDangerHiptop
Enjoy the fun at home!
Download the ASP.NET code from our new Google Code page!
The most important caveat is that you should thoroughly test the code for yourself based on your own needs and expectations -- and always using your high-priority target devices!
About the Contributor
Our gratitude to Brad Rigg, who wrote the initial port of the PHP code to ASP.NET and C#.
License Info & Costs
We have moved this code library to a Google Code hosting project and changed the license to an Apache License 2.0, which better fits a free, open source project like this. We've also rechristened this project "MobileESP." For more info, please visit the new MobileESP site: www.mobileESP.org