When “m.” is Not an Option

January 2nd, 2010

Over the past year at Notre Dame, we’ve really started to embrace and experiment with offering a mobile experience for a number of the projects that have made their way through our offices. There are a couple of obstacles that make this process difficult.

  1. A separate “m.” address is not an option
  2. Creating and testing mobile styles is rarely part of the project budget

This has led to some commonly used shortcuts that have allowed us to, at the very least, provide a serviceable mobile experience with very little effort.

The basic mobile stylesheet

At the very least, you should set up a ‘handheld’ stylesheet.

<link href="/stylesheets/mobile.css" media="handheld" rel="stylesheet" type="text/css" />

A number of things to keep in mind.

  • Keep your images under control by using width/max-width.
  • Use display:none liberally for content that is of little or no use to your mobile visitors.
  • Your mobile site doesn’t need all the pretty of the non-mobile version. If you include a lot of images, you’re going to end up with frustrated users.

Now, how do you test this? If you have a WAP enabled phone, you’re all set. I don’t. I have, however, found that Opera’s small screen view will give you a fairly accurate reading on your sites mobile friendliness. All you need to do is select “View > Small Screen”. MobiForge also has a guide to mobile emulators that you may find handy.

Targeting the iPhone/iPod Touch (or Webkit)

Here’s where the fun starts.

<link rel="apple-touch-icon" href="/images/iphone.png" />
<meta name="viewport" content="width=device-width" />
<link href="/stylesheets/iphone.css" media="only screen and (max-device-width: 480px)" rel="stylesheet" type="text/css" />

At this point, you can go one of two routes:

  1. Over-ride your existing screen stylesheet to make everything look nice on the iPhone, or
  2. Use javascript to eliminate your screen stylesheets and start fresh.

I highly advise you go with the latter. Here’s why.

For any sizable site that has a lot of complex css, you’ll find you’re spending way too much time tracking down which element in your screen stylesheet is blowing-out the width of your iPhone display. It’s much easier to start from scratch, and simply borrow from main css. Remember, we’re going for speed here. This isn’t in the budget.

Here’s how:

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
  for (var i=0; i<document.styleSheets.length; i++) {
    if (document.styleSheets[i].href != null && document.styleSheets[i].href.lastIndexOf("iphone") == -1) {
      document.styleSheets[i].disabled = true;
    }
  }
  window.scrollTo(0,1);
}

This simply loops through your stylesheets and disables all but your iphone.css. If you want to target additional webkit browsers, then you need to add them to the if statement:

if(
  (navigator.userAgent.match(/iPhone/i)) ||
  (navigator.userAgent.match(/iPod/i)) ||
  (navigator.userAgent.match(/Android/i)) ||
  (navigator.userAgent.match(/webOS/i))
) {

One of recent side project sites, on which I was able to spend time with the iPhone css was Game Day. Currently the iPhone/iPod account for 4% of the sites total traffic. The next mobile platform is Blackberry coming in at 0.26%. So yes, spend some time on your iphone.css. If you don’t have an iPhone or iPod to test on, download the latest iPhone SDK from Apple (membership to the developer connection is free). It includes an iPhone emulator that will work just as well for testing.

One of the great things about playing with custom styles for the iPhone is you get to play with all those fancy new Webkit and CSS3 features you can’t use in your day-to-day sites. So go ahead, you now have permission to experiment with gradients, drop shadows, RGBA, etc. Now if only we could serve up these sites as html5.

RVM Ruby 1.9 and the MySQL Gem — Friends at last on x86_64

November 13th, 2009

One of our production Rails apps is running on Ruby 1.9. The problem this introduces is that our dev environments must be able to run multiple versions of Ruby side-by-side. There’s nothing unique about this, it comes with the Ruby/Rails territory. After upgrading to Snow Leopard, I decided to give Ruby Version Manager a try. The install of RVM and the subsequent Ruby installs were a breeze. However, when it came time to install the MySQL gem, I ran into a brick wall. No matter what I tried, I kept getting install errors.

I’m running the standard MacOSX package install from mysql.com (Mac OS X 10.5 x86_64). I tried setting my ARCHFLAGS to the correct architecture, pointing to the correct config, and nothing helped. After Google turned up nothing, I decided to bother the nice folks in the #rvm channel. There I was given a handy little tip that goes as follows:

  1. Uninstall the version of Ruby you’re trying to install the MySQL gem on
  2. place the following in your ~/.rvmrc file:
    rvm_archflags="-arch x86_64"
  3. Reinstall Ruby using RVM
  4. Install the ruby gem:
    export ARCHFLAGS="-arch x86_64" ; gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

    (use the correct path to your MySQL config file. If you installed mysql with MacPorts, it will be in /opt somewhere).

The problem turned out to be that RVM was not installing the x86_64 version of Ruby 1.9. And if Ruby’s architecture doesn’t match MySQL’s, then you have problems. Once they match, then it’s back to Ruby bliss.