Mobile Development: Detecting Devices & Features
Join the DZone community and get the full member experience.Join For Free
take this post cum granlis salis. i’m trying to figure this stuff out and i’m thinking out loud.
whenever a browser makes a request, it includes a string identifying itself to the server. we commonly refer to this as the user agent string . this string identifies the browser and the platform and the version and a great deal more such nonsense.
this sounds great in theory. we should be able to use this data to optimize what’s being sent to the (mobile) browser. however, there’s been something of a sordid history for user agent strings . in retrospect, we’ve realized that user agent sniffing is a tool that has often hurt more than it has helped.
we’ve learned to favor feature detection over browser detection (or device detection). take a look at modernizr and haz.io for more on the that front. the success of feature detection has also resulted in a shift from server logic to client logic. we detect features on the client but we detect user agent strings on the server, before we send anything to the client.
how does all this play into the mobile web? one of the key mobile features we are interested in is screen size . luckily for us, the w3c has blessed us with media queries . in a nutshell, media queries allow you to conditionally apply css based properties of the display device. this has given rise to something known as responsive web design . responsive web design is about having a single set of markup whose layout can respond to the device’s display capabilities. unfortunately, there are a few rough edges with this approach.
in the mobile world, client-side feature detection has a few drawbacks. it requires extra code to be sent to the browsers and it takes additional processing on the client. it’s also likely that you’ll end up sending more than is really needed (or that you’ll need to make additional requests).
one solution to this conundrum is to use the open source “database” called wurlf . you can query wurl with a user agent string and it will return a set of capabilities. i think of it as feature detection on the server. though admittedly it’s a bit misleading to call it that.
this means your server can ask questions like “does this client support html5? if no, what do they support?” before the first response is even sent.
wurlf has commercial support and a c# api . for asp.net developers, 51degrees has an open source project called foundation that is built on top of wurl. it uses an httpmodule to automatically query wurl and populate the request.browser . setting up wurlf without foundation takes a little bit more work, but not too much. both are available on nuget: wurl and 51degrees .
what should you do?
i don’t think that there is a cut and dry answer at the moment . what you do depends heavily on your target audience. if you are targeting the north american market there’s a good chance you’ll be okay with a single set of markup, going with a responsive mobile-first design. in other words, there would be no need for something like wurlf.
however, you might need a very broad reach or you might be targeting a market heavy in feature phones or something else that’s very different from north america . in those cases, it is good to understand your options.
Published at DZone with permission of Christopher Bennage, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.