Week 17: WURFL
Introduction
Incompatibilities within mobile devices
- Last week we looked at some of the considerations we need to make if we are developing pages for mobile devices
- Even within the realm of mobile web development, there is incompatibility between different devices
- For example:
- Some mobile devices support XHTML, others support WML
- Different mobile devices have different screen resolutions
- Different mobile devices run different browsers
- Different browsers for mobile devices have different capabilities (e.g. some support GIF images, others support PNG images, some support AJAX, etc)
So...
- Clearly, it is quite a challenge to develop websites which are compatible amongst a range of different mobile devices
- How then do we solve this problem?
User-agent detection
- We can detect the user agent (basically, the browser) that a user is using to view the website
- Having detected the user agent, we can then generate different content for different devices
- Detecting a user agent in PHP:
<?php $a = $_SERVER['HTTP_USER_AGENT']; echo $a; ?>
- This will give an output such as:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
- There is a remaining problem, however: there are so many different mobile devices and browsers that you would have to test for many different user agents...
The WURFL: A repository of device capabilities
- We can, however, take advantage of the fact that many different user agents have common capabilities
- For example, several different mobile devices support XHTML, and of those, several support AJAX
- THe WURFL (see here) aims to address this issue
- This is:
"an XML configuration file which contains information about capabilities and features of several wireless devices"
(wurfl.sourceforge.net) - In other words, the WURFL is an XML file which maps individual mobile devices to their capabilities (e.g. do they support XHTML?)
How the WURFL is used from web applications
- WURFL can be used from a range of languages such as PHP and Java (see the WURFL site)
- From PHP the following procedure is performed:
- The user agent is obtained using $_SERVER['HTTP_USER_AGENT'] (see above)
- The WURFL XML file is loaded into memory: specifically into the WURFL object (a PHP variable which can be queried)
- The user agent is passed into the WURFL object
- We can then use the WURFL object to find out the capabilities of our client device
Example of using the WURFL from PHP
See here for a full example. Here is an explanation of each line of code:
-
$WURFL = new wurfl_class($w, $wurfl_agents);
This creates the WURFL object (see above) -
$WURFL->GetDeviceCapabilitiesFromAgent($_SERVER['HTTP_USER_AGENT']);
In this line, we use $_SERVER['HTTP_USER_AGENT'] to get the user agent string and feed it into the WURFL object. This enables us to query the WURFL object for the device capabilities -
if ( $WURFL->browser_is_wap )
This if statement determines whether this is a WAP browser. It needs to come after the GetDeviceCapabilitiesFromAgent as it is that line which works out what capabilities the browser has - If the if statement is true, we then generate WML. If not, we generate standard HTML (for a standard desktop browser)
-
if ( $WURFL->getDeviceCapability('gif') )This line determines whether the mobile browser supports GIF files. In the example, we display the image as a GIF if it does, or a WBMP if it does not.
More on Capabilities
The most important concept in the WURFL is the capability. A capability is a particular feature that the mobile device may or may not support, and we can query any capability. The way in which we reference a capability is:
$WURFL->getDeviceCapability(capability)We saw an example of this above where we queried the device for its gif capability, i.e. its ability to display GIF files.
Full documentation of all capabilities is available here. There are a number of capability categories, which are described in detail on the WURFL site; they include such things as:
- Various WML capabilities;
- Various XHTML capabilities;
- CSS capabilities;
- AJAX capabilities;
- Support for Java ME (Java for mobile software development)
Some specific examples:
- j2me_midp_1_0 true if the device supports J2ME (to MIDP 1.0 specifications)
- wav true if the device supports the .wav sound format
- wml_1_3 true if the device supports WML 1.3
- preferred_markup preferred markup language (e.g. WML or XHTML-MP) for this device - if more than one is offered, the newer (e.g. XHTML-MP rather than WML) will be returned
- ajax_support_javascript - does the device support JavaScript?
- ajax_support_inner_html - is the innerHTML feature of JavaScript supported? i.e. can you use innerHTML to stick content into a <div> ?
The WURFL cache
- When using WURFL in "real world" applications it is necessary to set up a cache
- The WURFL XML file is very large, so parsing (reading) it is slow
- With a cache, the WURFL is read in the first time only, then written to a cache file
- The cache file contains the WURFL data stored as a PHP array
- This much faster than parsing the XML as the cache file stores the data in the internal format used by PHP, rather than XML
- The second time the data is read, the cache file is used
- More details here.
UAProf
- UAProf is an alternative approach to user-agent detection
- WURFL is community-led (anyone can contribute to, or correct, the WURFL configuration file)
- UAProf, by contrast, is driven by providers of mobile devices
- Each mobile device manufacturer provides a UAProf describing the capabilities of each of their devices
- Each device has its own UAProf
- A UAProf (=User Agent Profile), like the WURFL, is an XML configuration file
- UAProfs for different devices have been added to the WURFL (remember the WURFL is a central repository to describe the capabilities of all devices)
How UAProf works
- When developing a mobile web application, you read the user agent string from the client device, and then retrieve the UAProf from a server typically belonging to the manufacturer
- Based on the contents of the UAProf, you deliver appropriate content to the client
- Thus crucially, UAProfs are stored on servers belonging to, and
maintained by, the device manufacturer
- This is an important difference from the community-led WURFL, which web developers can download and use directly
UAProf vs WURFL
- UAProf profiles are retrieved from manufacturers' web servers
- By contrast, the WURFL can be placed on the web application developer's own server and queried directly
- Thus there is more dependency on external servers with UAProf
- Since WURFL is community led, it is perhaps more prone to errors
- However, according to wurfl.sourceforge.net UAProfs themselves may contain errors
- The community nature of WURFL allows fixing of errors and early addition of new device profiles