File Under: CSS, Mobile

Build an iPhone-Optimized Website With iUI

The iPhone may not yet have a majority share in the smartphone world, but it’s well on its way. And that means creating a version of your website tailored to the look and feel of the iPhone is all but mandatory for today’s web apps.

It might seem like a pain to completely recreate your website just for the iPhone, but luckily doing so isn’t all that tough. The secret is to use the JavaScript toolset developed by Joe Hewitt. Hewitt’s script, known as iUI, takes care of all the heavy lifting for you.

So long as your site is using valid (X)HTML, it’s not hard at all to sequester off a domain for your iPhone users and apply the iUI tools to generate your iPhone-friendly site. Even better, you don’t need to be a JavaScript guru to get a basic site up and running.

Grab the code from the link above and let’s get started.


Contents

  1. Detecting versus Destination
  2. Getting Started
  3. Using Lists
  4. Beyond Lists

Detecting versus Destination

You might be thinking of using some JavaScript to sniff out mobile Safari and serve up your iPhone site whenever your detect Apple’s mobile web browser.

Don’t do that.

Just because you’re offering iPhone users a dedicated interface doesn’t mean you should force them to use it. Many of them may want to access your normal site, and if you’re using a browser detection script to redirect Mobile Safari, they won’t be able to get to the main site.

A far better plan is create a subdomain (like m.twitter.com) or other specific iPhone-friendly URL. That way, those who like your iPhone interface can get it and those that want access to the regular site can site enjoy that as well.

Getting Started

The first thing to do is set up your subdomain or other URL. For example: i.mysite.com. When you’re doing this, keep in mind that the iPhone isn’t the easiest thing to type on. So, the shorter the URL, the better.

Now you just need to generate some HTML for that subdomain. If you’re using a database and template publishing system, it’s not too tough. Just call the same data you’d get for your main site and structure it in a manner that suits the iPhone.

This is where it helps if you actually have an iPhone, but if you don’t here’s a tip to get you started: lists! Lots of lists.

Using Lists

The iUI library has a very nice set of tools that will turn your lists into a sideways-sliding navigation interface that works very well with the iPhone’s finger gestures.

For instance, let’s say you have a number of categories on your site and you want iPhone users to be able to navigate through all your content by category. One sound strategy would be to generate markup like this:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>iUI Barrel of Monkeys Demo</title>



<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>

<style type="text/css" media="screen">@import "../iui/iui.css";</style>

<script type="application/x-javascript" src="../iui/iui.js"></script>

</head>

<body>

    <div class="toolbar">

        <h1 id="pageTitle">Barrel of Monkeys</h1>



        <a id="backButton" class="button" href="#"></a>

        <a class="button" href="#searchForm">Search</a>

    </div>

    <ul id="home" title="Categories" selected="true">

        <li class="group">B</li>



        <li><a href="#Bananas">Bananas</a></li>

        <li><a href="#Barrels">Barrels</a></li>

        <li class="group">M</li>



        <li><a href="#Monkeys">Monkeys</a></li>

        <li class="group">P</li>

        <li><a href="#PointySticks">Pointy Sticks</a></li>



	</ul>

</body>

</html>



With this markup, we’ve embedded the iUI scripts and CSS. Then we’ve told iUI to create an iPhone styled list, like so:


Image:Iui1.jpg


If you look at the screenshot you’ll notice that iUI automatically uses the class attribute “group” to build iPhone-style list dividers and styles all our links as horizontal sliding elements.

If you’ll look at the actual HTML, you’ll notice none of our links actually lead to other pages. Depending on the complexity of your site, this may not work for every page. However, for the most part, even though your users will feel like they’re paging through you site, you don’t actually need to load a new page. Your interface is speedier that way.

So let’s add some more content. Paste this code in just below the list code:

<ul id="Monkeys" title="Monkeys">

      <li><a href="#howler">Howler</a></li>

      <li><a href="#spider">Spider</a></li>

      <li><a href="#rhesus">Rhesus</a></li>



      <li><a href="#barbaryape">Barbary Ape</a></li>

</ul>

<p id="howler">Howler Monkeys love to howl.</p>

Now if you head to your page using a iPhone (or an emulator) and tap/click on the “Monkeys” name, the page will slide to the right and display our new list. Click on the “Howler Monkey” link and you’ll slide over to a blank page with our paragraph text.

Beyond Lists

Obviously, not everything you’re going to want to show on the iPhone is going to be a list. But don’t worry, iUI can help out with other stuff too. There are some HTML classes to handle common input elements like modal dialogs, preference panels, on/off switches and loads more.

It also neatly solves a common iPhone design issue — long lists. Apple’s apps get around the problem of long lists by including a “load X more” link at the bottom of a short list. And that’s exactly what iUI allows you to do as well. Just create a link with target=”_replace” and iUI will load the URL it and replace the <a> with the contents of the URL.

Of course you’ll probably find some of your content falls outside the bounds of iUI and in the end you may have to write some CSS yourself. But for handling common cases on the iPhone, iUI is an indispensable resource.