MOBILE

Building Android Apps : Detecting Browsers with WURFL

1/17/2011 5:44:45 PM
WURFL (Wireless Universal Resource File) is an XML file that contains the information needed to identify a wide range of mobile devices. On its own, it doesn’t do anything. But if you use one of the many available libraries for it, you can create web apps that can figure out what kind of device has connected to your app.

For example, wurfl-php (http://sourceforge.net/projects/wurfl/files/WURFL%20PHP/) lets you detect which operating system a remote device is running from within a PHP script.


Note:

To use WURFL and wurfl-php, you’ll need to be running your web app on a hosting provider that supports PHP. You’ll also need to understand how to install files and PHP libraries onto your server. In this appendix, I show you how to do this using the Unix or Mac OS X command line. If you are uncomfortable with any of this, but are comfortable working with PHP, contact your hosting provider’s support department and ask if they’d be willing to install WURFL and wurfl-php on the server you use. If you’re using a shared server, it would give your hosting provider a competitive advantage to offer this feature to all their customers.


1. Installation

First, download wurfl-php and unzip it somewhere on your server (in general, it’s best to not put libraries in your public web folder, so I’m putting it into the src~/src with the location you want to install it to and replace wurfl-php-1.1.tar.gz with the name of the file you actually downloaded: directory in my home directory). Replace

$ mkdir ~/src
$ cd ~/src
$ tar xvfz ~/Downloads/wurfl-php-1.1.tar.gz

Next, download the latest WURFL file (http://sourceforge.net/projects/wurfl/files/WURFL/), copy it into the wurfl-php folder, and gunzip it (see the wurfl-php documentation for tips on using this file in its compressed state). Replace ~/src/wurfl-php-1.1/ with the full path to the directory that was created in the previous step when you extracted the wurfl-php distribution, and replace ~/Downloads/wurfl-latest.xml.gz with the path to the WURFL distribution that you downloaded:

$ cd ~/src/wurfl-php-1.1/
$ cp ~/Downloads/wurfl-latest.xml.gz .
$ gunzip wurfl-latest.xml.gz

Finally, download the desktop web browser patch so WURFL doesn’t encounter errors when someone visits your page from a desktop browser:

$ curl -O http://wurfl.sourceforge.net/web_browsers_patch.xml

2. Configuration

Create the following wurfl-config file (wurfl-config.xml) in ~/src/wurfl-php-1.1/ (or the directory you created when you extracted wurfl-php):

<?xml version="1.0" encoding="UTF-8"?> <wurfl-config> <wurfl> <main-file>wurfl-latest.xml</main-file> <patches> <patch>web_browsers_patch.xml</patch> </patches> </wurfl> <persistence> <provider>file</provider> <params>dir=./cache</params> </persistence> </wurfl-config>

Create a cache directory and make sure it’s writable by whichever user runs PHP scripts. If your web server is configured to run your PHP scripts under your user credentials, this step should not be necessary. As with previous examples, replace ~/src/wurfl-php-1.1/ with the location you created earlier. Replace _www with the username that your PHP scripts run under (you will need superuser credentials to run this command):

$ mkdir ~/src/wurfl-php-1.1/cache $ sudo chown _www ~/src/wurfl-php-1.1/cache

Note:

If in doubt, contact your hosting provider’s tech support and explain you want the cache directory to be writable by your PHP scripts.


3. Testing wurfl-php

Now, in your web directory (such as Sites or public_html), create the following PHP file (name it something like wurfl-test.php). The first time you visit it from your Android device (or any other browser), it will take a long time as it builds the initial cache. After that it should be zippy. Figure 1 shows how this should appear in your browser. You can now modify this PHP code to suit your needs:

<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width" />
<title>WURFL Test</title>
<?php

define("WURFL_DIR", "/Users/bjepson/src/wurfl-php-1.1/WURFL/");
define("RESOURCES_DIR", "/Users/bjepson/src/wurfl-php-1.1/");

require_once WURFL_DIR . 'Application.php';

$wurflConfigFile = RESOURCES_DIR . 'wurfl-config.xml';
$wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);
$wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);

$wurflManager = $wurflManagerFactory->create();
$wurflInfo = $wurflManager->getWURFLInfo();

$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
$is_android = FALSE;
if ($requestingDevice->getCapability("device_os") == "Android") {
$is_android = TRUE;
}
?>
</head>
<body>
<?php
if ($is_android) {
echo "I spy an Android phone.";
}
?>
<ul>
<?php
foreach ($requestingDevice->getAllCapabilities() as $key => $value) {
echo "<li>$key = $value";
}
?>
</ul>
</body>
</html>



Note:

I couldn’t use ~, so I had to put in the full path to the WURFL stuff; replace /Users/NAME/src/wurfl-php-1.1/ with the full path to the wurfl-php directory you created earlier.


Figure 1. Output of the sample wurfl-php script


Other  
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
  •  Windows Phone 7 Development : Using Cloud Services As Data Stores - Creating a Cloud Database
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 5) - Implementing the View Controller Logic
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 4) - Hiding the Keyboard
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 3) - Creating Styled Buttons
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 2) - Adding Text Views
  •  iPhone Application Development : Working with Text, Keyboards, and Buttons (part 1) - Adding Text Fields
  •  Building Android Apps : Controlling the Phone with JavaScript (part 3) - Accelerometer
  •  
    Most View
    The AG2712 - The New All In One PC
    SSD Test: Samsung SSD 840 Pro 256GB vs. PNY Prevail Elite SSD 240GB
    The End Of Wintel (Part 2)
    What We Love Most This November?
    Dell Latitude E6430 ATG - All-Day Battery Life
    Intel Core i7-4770K CPU Review - Intel Haswell For Desktop (Part 5)
    Ten Top Synths (Part 1) : Native Instruments, KV331 Audio, LennarDigital
    B&W Adds P7 Headphones With Clever Folding Design
    Windows 7 : Developing Applications with Enhanced Security - DEVISING AND IMPLEMENTING A SECURITY POLICY
    How To Draw 3D Models With SketchUp
    Top 10
    Windows Server 2008 R2 : Active Directory certificate services (part 2) - Deploying Active Directory Certificate Services
    Windows Server 2008 R2 : Active Directory certificate services (part 1) - Planning for Active Directory Certificate Services
    Windows Server 2008 R2 : Administering group policy (part 2) - Creating and managing Group Policy Objects, Troubleshooting Group Policy
    Windows Server 2008 R2 : Administering group policy (part 1) - Overview of Group Policy
    Windows Server 2008 R2 : Administering groups and organizational units
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 3) - Adding Items to Public Folders Using Outlook
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 2) - Determining Public Folder Size, Item Count, and Last Access Time
    Microsoft Exchange Server 2010 : Creating and Working with Public Folders (part 1)
    Microsoft Exchange Server 2010 : Accessing Public Folders Through the Information Store
    SQL Server :ONE-WAY ENCRYPTION - Creating the Interface (part 2) - Setting and Verifying Permissions to the Stored Procedures