Archive for iPhone

Hack your iPhone: toolchains and cross-compilers

The iPhone can run “web apps” very easily. These web apps are really just specially-crafted web pages that take advantage of the iPhone’s built-in web browser. Such web apps can be very cool, but they don’t provide the full functionality that you can get from an actual executable program running on the iPhone. A “native app” that runs on the iPhone can read the iPhone’s built-in accelerometer, for example.

So lots of people want to write independent/third-party native apps that run on the iPhone. The problem is that while Apple hasn’t squashed the development of native apps, they haven’t helped, either. For example, Apple hasn’t provided a toolchain to compile programs for the iPhone.

What is a toolchain?

Yeah, I know, it’s been I while since I had my compiler class too. Here’s a rough diagram of what happens when you compile source code into a program:

Compilation process: compiler to assembler to linker

Here’s the process as I recall it. You write code in high-level computer languages such as C, C++, or Objective-C. That gets piped through a compiler (”gcc” in this diagram), which creates assembly code targeted for a certain type of machine architecture such as Intel x86, PowerPC (PPC), or ARM. Most desktop machines, including newer Apple computers, use Intel or Intel-compatible chips. Older Apple computers use PowerPC chips. ARM chips are embedded in lots of popular consumer electronics devices (e.g. the iPod and iPhone).

Now you’ve got assembly code for a particular type of computer chip. That code is processed through an assembler (”as” in this diagram), which spits out actual machine code (also called “object code”) of zeros and ones. In some cases that code can be executed directly. However, usually you have multiple files of object code to combine, or you need to link in built-in functions from system libraries. A linker (”ld” in this diagram) pulls all the object code into one executable program that a user can run.

So what is a toolchain? Roughly, a toolchain is a set of programs that allow you to compile source code into a program. It can include the programs that I just mentioned, plus a host of other programs such as “make” or a debugger that make compiling programs easier.

What is a cross-compiler?

Look back at the diagram above. Typically someone on (say) a newer Apple computer would have an Intel chip inside. So gcc would output assembly code for an Intel chip, and that assembly code would eventually be transformed into an executable program that runs on a Intel chip. That lets you compile and run programs on your own computer.

But nothing in the diagram above says that you have to output assembly code for the same type of computer chip as the machine that you’re running. For example, an Intel-based Apple computer could translate source code into assembly code for an ARM-based machine architecture. That’s called “cross-compiling,” because you’re compiling your code across for a different type of chip.

Why would you want to cross-compile anything? Well, iPods and iPhones are based on ARM chips. Can you imagine trying to compile source code on one of those little devices? It would be a real pain. Cross-compiling solves that problem: you can compile code on a desktop machine for (say) the iPhone, then figure out how to copy your executable program over to the iPhone, and figure out some way to run your program.

What does this have to do with the iPhone?

It’s possible (but currently difficult) to make a working toolchain for Apple computers that cross-compiles code so that the iPhone can run it. Once you have a cross-compiling toolchain, you can write code that runs on the iPhone.

Unfortunately, Apple hasn’t provided any documentation on the application programming interface (API) or system libraries that would allow people to write native applications for the iPhone. Luckily, dedicated people have helped to produce not only a toolchain but also to fill in documentation on the system APIs that the iPhone supports. In the same way that Apple desktop machines have a framework called AppKit, the iPhone has a user-interface (UI) framework called UIKit.

More reading about toolchains/cross-compiling

If you dig deeper into nearly anything I just said about toolchains or cross-compiling, you’ll discover that it’s more complicated than I just laid out. For example, libraries of built-in system functions don’t have to be statically combined into a program. It’s possible to wait until the program is being loaded to execute before linking in a library. And so-called “binutils” provide lots of functionality other than compilation. For an overview of the compilation process, see the book Running Linux for a good description. To learn about linking vs. loading, I enjoyed this Linux Journal article. You can read more about GCC or binutils on the official GNU site. And of course Wikipedia provides a variety of strong articles about the GNU tool chain, linkers, and crosscompiling.

Comments (22)

Hack your iPhone: install applications with Installer.app and AppTapp

(By the way, when I talk about “hacking” your iPhone, I mean closer to “modding”. I’m interested in running third-party native applications on my iPhone, and don’t care as much about unlocking the iPhone to work with other carriers.)

I’m amazed at the pace of iPhone hacking. Even a week ago, you’d see multi-step guides to installing apps on an iPhone. Now there’s a program (Installer.app) with a front-end called AppTapp that does everything in a nice graphical user interface (GUI) for you. You don’t have to type a single command, but you will need an Apple computer (either Intel-based or PowerPC-based works).

Disable automatic sync in iTunes

First, plug your iPhone into your computer so that iTunes comes on. On the left-hand side of the screen under “Devices” click on your iPhone, and then on the page that comes up, click so that “Automatically sync when this iPhone is connected” is OFF. It looks like this:

Disable the synchronization in iTunes

(By the way, to grab a screenshot of a window on a Mac and save it to the desktop, you need to hit Command-Shift-4, then the space bar, then click a window. Sheesh.)

Download and run AppTapp

The combination of AppTapp and Installer.app lets you add applications to your iPhone with almost no effort:

- Make sure that iTunes isn’t running. Plug your iPhone into your Apple computer and make sure that iTunes didn’t start running.
- Download AppTapp to your Apple computer from http://iphone.nullriver.com/beta/
- Run it.

That’s pretty much it. iPhone Atlas has the best walkthrough I’ve seen if you want more details about AppTapp/Installer.app or what to do after you’ve run AppTapp. The short version is to update Installer.app first (touch the “Update” tab). Next you want to install the Launcher program (you can only see 16 icons on your iPhone, so installing Launcher ensures that one of those first 16 icons can access other applications).

After that, you’ve got a lot of options. The “Community Sources” package will give you even more choices for applications to install. Installing “OpenSSH” and “BSD Subsystem” is also recommended. Finally, if you install the MobileTerminal application, you can experience the joy of typing “ls” on your iPhone. Walking around with UNIX in your pocket is very nice. :) See the iPhone Atlas guide for screenshots and more info.

If you want to impress your fellow geeks, Lights Off was the first native game for the iPhone and it also looks great. My brother and I had a similar “turn the lights out” game when we were growing up, but it was called Merlin. Now you can play this game on much prettier handheld appliance. :)

More Resources

If you’re a Windows person and can’t beg/borrow a Mac from anyone, you might check out iBrickr. It lets you manage ringtones and applications on your iPhone.

If you don’t want to go the AppTapp/Installer.app way on a Mac, you might also check out iFuntastic. iFuntastic lets you tinker with all kinds of things, from ring tones to applications to multiple “home screens.” Each home screen can have a different set of icons. Version 3 was released just a few days ago and the new version adds PowerPC support.

As always, back up your data first, and any of this could (in theory) break your iPhone. I don’t think anything I’ve mentioned would get you in trouble with lawyerfolk, but if you’re worried, you can always play it safe and stick with the built-in applications on the iPhone. If you see errors above or know of another interesting way to install native third-party applications on an iPhone, please mention it in the comments.

Comments (56)

Hack your iPhone: back up your data

If you want to hack your iPhone, be prepared for the notion that you can lose all your data or (worst case) turn your iPhone into a useless brick — thus the origin of the word “bricking” a device. Normally if things go awry, the worst that will happen is that you have to restore your iPhone to its initial pristine state. That means that you’d lose your settings and data on the iPhone. So before proceeding with hacking your iPhone, make sure you save off your data as follows.

Windows users

- Back up contacts - I don’t use Outlook or Outlook Express, so my contacts are stored using the Windows Address Book (found with Start->Programs->Accessories->Address Book). I exported my contacts as a WAB (Windows Address Book) file and a CSV (comma-separated value) file.
- Back up photos - On my Windows XP system, iTunes doesn’t copy photos to my desktop computer (annoying!). And each time I plugged in the iPhone, a different dialog window would pop up (also annoying). It looked like this:

Microsoft wizard to suck down camera images

It turns out that these two annoyances cancel out. :) Go ahead and run the “Microsoft Scanner and Camera Wizard” and you can import the pictures on your iPhone and move them to your Windows computer pretty easily.
- Back up settings - This sounds horribly low tech, but grab a blank piece of paper and walk through your “Settings” application on the iPhone and just write down each setting. It’s boring, but in 10-15 minutes you’ve got a record of every setting on your iPhone, so it won’t hurt if you have to restore your iPhone to a pristine state.
- Back up music - I don’t buy music from iTunes; I use MP3s from my music collection. So I had copies of the music on my iPhone somewhere else and didn’t worry about backing up my music.
- Back up bookmarks - I use Firefox for my normal bookmarks and Internet Explorer for my iPhone bookmarks. I don’t have that many iPhone bookmarks, so I didn’t worry about saving a safe copy somewhere else.

Mac users

I assume that your iPhone syncs files to your Apple computer. To be cautious, make backups of whatever data you can by copying photos/file data to an extra-safe location. Any Apple users want to chime in with more specific advice?

Ready to go!

Okay, your data is backed up — you’re all set to begin hacking your iPhone. I’ve only had good experiences so far, but remember that things can go wrong. So back up that data first!

Comments (13)

iPhone user agent

The iPhone launched with this user agent:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3

Here’s a tip to find out the user agent for any browser or device that you’re on (e.g. a kiosk at a mall, a different cell phone, or something else that you tinker with):

- Go to Google
- Search for the word phpinfo. If you visit a webserver running the phpinfo() command, the resulting web page will tell you info about the web server, but it will also tell you about your browser’s user agent.
- Look for a page that actually runs phpinfo() instead of documentation about phpinfo(). Usually there’s a few such pages in the top 10. If you’re having trouble finding a phpinfo() page, you can do a more specific search for the words phpinfo http_user_agent.
- Click on the phpinfo() page and near the bottom of the page look for a string called HTTP_USER_AGENT. You’ll see something like this:

phpinfo user agent

Sorry that the picture runs in the right-hand column; I didn’t want to clip or resize the image. I took this picture running Firefox on my Ubuntu Desktop, so that’s the user agent you see. But this tip works with iPhones and almost any other browser that can search on Google.

Comments (45)

So I bought an iPhone..

My wife got an iPhone the day it came out. Robert Scoble got there the day before and slept in line at the Palo Alto store. We stopped by the Palo Alto store around 10 p.m. on iDay, waited less than five minutes, and got one of the last 8GB iPhones before they sold out. I decided to hold off on getting one to see how well the iPhone worked for my wife (she’s a Mac person and I’m… not).

Within a few days, I knew I was going to get an iPhone too. I got my iPhone this past Friday and I’ve got a lot to say about it. This post is more of a pre-warning that I’m thinking of doing more gadget/howto/iPhone blogging over the next few days. If you only read my blog to get thoughts on Google or search engine optimization (SEO), read this post from last year about how to subscribe to a feed that is restricted to Google/SEO posts.

Okay, now I feel ready to do some non-SEO posts. :)

Comments (68)