‘Your Next Bus’ on your iPhone

‘Your Next Bus’ is the system used in West Yorkshire (and other regions) to provide live bus timetables. Some time ago I wrote a Dashboard Widget to display live timetables on a OS X Dashboard. I had a request to develop this for the iPhone and so I knocked something together with Dashcode and came up with tyr.org.uk/bus. Browse to in in your iPhone and see it in action, requires Version 2.0 of the OS.

Please feedback with any bug reports or feature requests.

Read on for some of the behind the scenes info.

There are two options facing a prospective iPhone application developer, to develop a web app or a full blown iPhone application. Prior to the release of the iPhone 2.0 OS web apps were the only option open to developers wanting to target users without jailbroken phones.

Writing a full iPhone application requires Cocoa and Objective-C fu, which whilst I’m learning I’m nowhere near the level required this task. Conversely web apps are much simpler and are just served as standard web pages to the local iPhone Safari browser. All the client side coding can be created in JavaScript.

Apple’s provided development environment for web apps is called Dashcode. I’d used it previously as a beta on Tiger to create the Dashboard widget and found it to be fairly buggy. This time however I’m pleased to say it performed admirably. To aid development a selection of framework apps are provided, in this instance the ‘browser’ framework was picked. This provides you with a simple interface that lets the user browse through multiple view in a tree structure. The only Javascript you need to provide is a controller for the view, a sufficient outline is given to get you going without too much work.

I don’t actually own an iPhone but an iPhone simulator is provided with the development tools. As soon as you click ‘Run’ in Dashcode the simulator loads up and launches your web app. You can simulate pretty much all the normal iPhone functionality including rotation and multi touch. It really helps make development a breeze.

iPhone OS 2 ships with a version of Safari 3.1.1, which supports client side SQL in JavaScript. Using this to store user settings is a breath of fresh air compared to creating bespoke functions to (un)pack data into cookies. Here’s the SQL used to create a database

 try {
     var shortName = 'yournextbus';
     var version = '1.0';
     var displayName = 'Your Next Bus Database';
     var maxSize = 2048;
     var db = openDatabase (shortName, version, displayName, maxSize);
     return db;
 } catch (e) {
     if ( e == INVALID_STATE_ERR ) {
         // Version number mismatch
         alert ("Invalid database version");
     } else {
         alert ("An error occured creating a database "+e+".");
     }
 }

And here’s the code to create a table and add a row:

 this._db.transaction (
    function (transaction) {
        transaction.executeSql('CREATE TABLE stops(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT "A Bus Stop", number INTEGER NOT NULL)', [], nullDataHandler, killTransactionSilently);
        transaction.executeSql('INSERT INTO stops ( name , number ) VALUES (?,?) ', [ 'Demo Stop', 45013610 ], nullDataHandler, errorHandler );

     }
 )

Whilst the data being stored for this app is fairly trivial it’s simple to see how this could become very powerful.

A couple of pics of the app in action:

Main Menu
Timetable

8 Responses to “‘Your Next Bus’ on your iPhone”

  1. nik Says:

    Just a quick note to say keep up the good work. Just come across this page whilst trying to get timetable for my boys first day at school. Now I have the times for the 35 & 71 buses stored he should never be late!

    It would be great to see this as an application. It could do with the ability to change names of stops and the ability to link to the PDF of the full bus timetable to make it a journey planner as well, but none the less a really nice little app.

    Cheers

  2. Mike Says:

    This app is fantastic, I use the WYMETRO busses daily and now it’s so much easier to ceck my bus stops, you’re a life saver!

  3. Nathan Massey Says:

    I was just about to crack open Textmate and get writing my own version of something like this, but I’m glad I had a quick Google first, as this is exactly what I’m after.

    Like nik said, keep up the good work. It might be worth looking into doing a ‘proper’ app using the SDK. Add a few more features in there like a journey planner, stick it on the App Store for about £1.19 and boom, you’ve got a nice little earner going on!

  4. Mike simpson Says:

    Just come across this today. It is a great app. Saved as a bookmark in iPhone. Oh please do an application alternate. It’s the only thing missing. Ps anyone having trouble finding bus stop numbers, they’re on the bus stop sign or go to wymetro.com.

  5. Sam gall Says:

    Hi there.. Loved this bus app on my iPhone but I’ve recently upgraded to a blackberry bold 9700.. Is there any way to use this Leeds bus service on my blackberry bearing in mind blackberrys are also extremely popular nowadays too!..

    Cheers Sam

  6. mark Says:

    try to get another iphone sam. if not a pocket timetable

  7. Jiri Says:

    Hello! I have to say your bus app is really a live saver. It helped me so many times and I was simply helpless when it stopped working in Fabruary.

    Now when it is back again, I can thank you for the fantastic job you have done!

    Thank you and please keep it running :)

  8. jacketfullofdanger Says:

    This seems to work a treat on Android phones as well. Incredibly useful, thanks!

Leave a Reply