The 2.3 release of DeviceAtlas brings with it the next major release of our API. Version 2.3 is being released as a Beta to allow users to familiarise themselves with the new features and adapt their implementations to take advantage of these.
“What happened to version 2.0?” you may ask. Well, our product managers got very upset when we wanted to call this version 2.0. “We had our 2.2 release weeks ago!” they gasped in horror. To be fair they are right. You see the website and data at DeviceAtlas.com have been the focus of our love and attention for the past few months and have flown ahead to version 2.2. Well, today is release 2.3. No changes to the website but a bright shiny new API for all of our developers so here you go.
We’re very excited about the direction the API has taken for this major release. There are some great new classes available which we think make using DeviceAtlas as simple as a single line of code. Seriously. Not only that but we’ve added a whole bunch of new detection intelligence to our data that will make your device detection even better.
Easy to use – even for designers…
We know that before we made you do a lot of the ground work yourself using the DeviceAtlas API. Finding the headers, looking out for those sneaky transcoders and proxies not to mention caching your device data. Well with the new API we will handle all the caching, and header “sniffing” for you, making your device aware development a piece of cake.
Here’s some sample code to show you how easy it is:
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Configuration $config = array( 'json' => 'path/to/File.json', //Path the JSON file 'cache' => 'file:///tmp/deviceatlas_cache', //Optional cache 'defaults' => array( //Setup defaults for unknown values 'displayWidth' => 128, 'displayHeight' => 128, 'vendor' => "Unknown Vendor", 'model' => "Unknown Model" ) ); //Create Client object $device = new Mobi_Mtld_DA_Client($config); //Get display width and defualt to 128 if unknown (see config above) $displayWidth = $device->displayWidth; |
Java – Sample JSP code
1 2 3 4 5 6 7 8 9 |
<%@page import="mobi.mtld.da.servlet.*" %> <% //Create Client object ServletClient device = new ServletClient("path/to/File.json", request); //Get displayWidth and defualt to 128 if unknown int displayWidth = device.property("displayWidth", 128); //Get vendor and return null if unknown; string Vendor = device.property("vendor"); %> |
.NET – Sample ASP.NET code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ Import Namespace="Mobi.Mtld.DA" %> <%@ Import Namespace="Mobi.Mtld.DA.Web" %> <% //Create Client object //Read JSON path from web.config //Get headers from current http request HttpClient Device = new HttpClient(Request); //Get displayWidth and defualt to 128 if unknown int DisplayWidth = Device.Property("displayWidth", 128); //Get vendor and return null if unknown; string Vendor = Device.vendor; %> |
Server Additions – making device detection… invisible
The Java and .NET API’s come with some new packages/namespaces that contain classes specifically for use within the ASP.NET or Java Servlet environments. These server extensions can be configured to handle incoming requests before they are passed to your handling code and add extra device data from DeviceAtlas.
With the server extensions installed and configured you can forget DeviceAtlas is even there. Just code your sites and refer to device capabilities like they are second-nature.
This means really simple code can be implemented within your solutions, simple enough that we think it will even make your designers happy.
Java – Using Expression Language
${browser.mobileDevice ? "
Visit our Mobile Site.
" : ""}
ASP.NET
1 2 3 |
<% if (Request.Browser.IsMobileDevice) { %> <p>You appear to be a mobile user. Why not visit our <a href="http://example.mobi">Mobile Site</a>.</p> <% }%> |
A bonus for PHP devs on IIS7
An added bonus of this is for PHP developers hosting on IIS 7. Because IIS7 allows you to activate modules written in managed code for applications that operate outside the ASP.NET environment you can get DeviceAtlas data directly within your PHP applications. So you could write PHP code like this:
1 2 3 4 5 |
if(isset($_SERVER["HTTP_X_DEVICEATLAS_DISPLAYWIDTH"])){ $displayWidth = intval($_SERVER["HTTP_X_DEVICEATLAS_DISPLAYWIDTH"]); } else { $displayWidth = 128; //Default to 128 } |
Even better device detection
We know our device data is pretty damn good already but we’ve gone a step further to make device detection with our API even better. We’ve have added UA Profile data to the JSON and the capability to use this data to the new API. Also the API will automatically sniff out the correct headers for you, so no more checks for proxies or transcoders.
This is a quatum leap for DeviceAtlas as we leave the old “purely User-Agent based” device detection days (and most other device detection systems) behind us. Remember to get this added detection accuracy you need the new JSON file that comes with the API. The downloadable JSON will continue to be in the old format until this API becomes the official release.
The example below illustrates this improvement using PHP but applies to all 3 API’s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<pre> /* This creates the headers object from all the headers it can find in the current request. NB: This is the recommended approach as the API will select the best headers to use */ $headers = new Mobi_Mtld_DA_HttpHeaders(); /* Override the headers to show how the new UAProfile data helps improve recognition when the UA may be generic and used by a number of devices */ $genericUA = "Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)"; $uaProfile1 = "http://www.htcmms.com.tw/gen/Wizard-2.0.xml"; $uaProfile2 = "http://downloads.palm.com/profiles/Treo750R1.rdf"; $device1 = new Mobi_Mtld_DA_Client('path/to/File.json'); $device1->setHeaders(new Mobi_Mtld_DA_HttpHeaders($genericUA, $uaProfile1)); $device2 = new Mobi_Mtld_DA_Client('path/to/File.json'); $device2->setHeaders(new Mobi_Mtld_DA_HttpHeaders($genericUA, $uaProfile2)); echo "Device1 is a " . $device1->vendor . " " . $device1->model . "n"; echo "Device2 is a " . $device2->vendor . " " . $device2->model . "n"; /* This will output: Device1 is a HTC Wizard Device2 is a Palm Treo750w */ |
Download
You can download the API, samples and documentation from the DeviceAtlas website.
Unfortunately the Ruby and Python API’s are not ready for release yet but we just couldn’t wait any longer to show you how cool the new API’s are so we wanted to get these out anyway.
Feel free to comment on your experience using the API here or in the forums. We welcome your feedback