So I Made My iPhone Say Hello, World…
…with the open source toolchain. It was a real pain until I decided to focus on the easy way. Then it wasn’t hard at all.
For reference, the hard way is on iphone-dev. I spent most of a frustrating, unsuccessful day with that page and its thousands of comments, especially extracting the SDK from the 1GB XCode binaries - you want version 2.5, not a newer version, and use PowerISO to extract it. For what it’s worth, there looks to be an easier way for Cygwin on Windows.
The Easy Way
But the really easy way is to install the development tools on your iPhone.
This requires:
- a “jailbroken” phone - so you can install open software on it.
I used Windows QuickPwn and it was easy. Works with the 2.1 firmware, too…
- a Wi-Fi connection - and get the IP address of your iPhone:
- open the iPhone Settings application
- tap Wi-Fi
- tap the (>) button to the right of your active connection
- note the IP Address - probably something like
192.168.1.101, which I use below
- an SSH client of your choice - Linux and Mac come with ssh clients.
Windows users can download PuTTY or use the Cygwin Unix layer on top of Windows. Developers running Windows may also want to grab WinSCP for its “Keep Remote Directory Up To Date” feature (more on that in another post).
After the Jailbreak
After your phone is jailbroken, open the Cydia application. It is like the AppStore, but for open software.
Tap “Featured Applications”, then flick to the bottom, and tap OpenSSH, then tap Install, tap Confirm.
When the installation completes, go back to Cydia, tap Search in the bottom-right, and type toolchain. Tap iPhone 2.0 Toolchain, then Install then Confirm. This is a pretty big iPhone application - over 40MB expanded! So, while your iPhone is working on that, let’s go back to the desktop machine.
You should now be able to use ssh (or PuTTY…) from your desktop computer to connect to your iphone, using root as the user, your iPhone’s IP address for the host, and alpine for the password. If you’re using a graphical SSH client like PuTTY, follow its prompts. On Mac/Linux/Cygwin, open a terminal, and type:
ssh root@192.168.1.101
root@192.168.1.101's password: [type alpine]
The first time you connect, it may take about 30 seconds, then you should then see a Unix command-line prompt from the iPhone, and it will end with a “#” - which means you are the “root” or administrative user. You’re the boss - congratulations!
First thing you should do as boss is make sure nobody else can be boss - change your password! (No, really, change it now - here’s how…)
passwd
Changing password for root.
New password:
Retype new password:
Don’t forget this password.
If you do, you’ll probably have to jailbreak the phone again to reset it.
After the Toolchain is Installed
OK, so you can ssh into your iPhone, and you’ve installed the toolchain (that installation has completed on your phone, right?).
So, on your desktop with the ssh window into your iPhone, type:
echo 'main() { printf("Hello, iPhone!\n"); }' > hello.c
gcc hello.c -o hello
ldid -S hello
./hello
And your phone should say:
Hello, iPhone!
When I first did this, it was late at night, and I forgot the ldid -S hello, so instead of “Hello, World”, I got:
# ./hello
Killed
The iPhone will only run “signed” code, and since ./hello was not signed, the iPhone killed it. The “ldid -S PROGRAM” signs the program for you. Turns out the iPhone doesn’t care what signature is on the file, just that it is marked with a signature…
So there you go: a usable toolchain on your iPhone, waiting for you to write the next great iPhone program. For real development, you’ll want to use your desktop development environment and something like sshfs, rsync, or WinSCP to copy the files to the iPhone as you edit them, but more on that later… Updated: More is now available for Windows/WinSCP and Linux/sshfs
