You’ve been downloading other people’s apps on your iPhone, and now you want to know what it takes to create your own. If you’re a beginner with some prior programming experience, you’ve come to the right place. Though there are many “Hello World” tutorials on the Internet, this one is designed to get you on the iPhone simulator with as little fuss as possible, while also teaching you how an iOS app starts up and loads the first screen.
When you open up the sample project in Xcode (by double-clicking hello.xcodeproj), you’ll see a folder hierarchy with source code files underneath ‘hello’.
In this sample there are three types of files: .h files, which simply outline the interfaces to objects, both visual and non-visual. Then there are .m files, which contain the actual code for those objects. And finally there are .xib files, which not only control the layout of buttons and labels, but also link them to properties and methods defined in .h files. From within these files we’ll be able to see how the app starts up.
All iOS apps begin in main.m, which simply launches the piece of code responsible for initializing your app, the AppDelegate. There is rarely any customization applied to main.m, and you may safely ignore this file in any other samples that you download (it’s often tucked away in the Supporting Files folder). The AppDelegate, however, is very important.
Outlined in AppDelegate.h and implemented in AppDelegate.m, the AppDelegate does not have any visual elements. After it loads app preferences and other objects, it serves as the central access point for any global functions and values needed throughout the app’s lifetime. In this sample, the AppDelegate provides a playPopupSound method.
When the AppDelegate is done initializing in application:didFinishLaunchingWithOptions, it loads the first screen, called ViewController.
The ViewController not only has the standard .h header and .m implementation, it also has two .xib files for the screen’s user interface on iPhone and iPad. The newViewController method in ViewController knows which .xib to load. Subsequently, the loadView method automatically hooks up the elements in the .xib file to certain properties and methods defined in the .h file.
There are a few more events iOS apps may utilize during startup, when necessary, but for our sample the startup process is complete!
Now watch all of this happen on the iOS simulator by pressing the Run button at the top-left of the Xcode window. The sample contains several NSLog() statements so that you can see the events described above in the Xcode debug window as they occur.
Once the ViewController has completed its loadView method, you’re free to interact with the app, such as clicking and moving the button on the screen. Note how the ViewController calls the playPopupSound method in the AppDelegate whenever the Move Me button is pressed.
If you’ve gotten this far, you may want to take on the TODO assignments I’ve scattered throughout the code, which ask you to implement a popdown sound within the app. It should be relatively easy, and there are only 3 files in the project you have to modify. Once you are done you can download this version of the sample to see the answer.
Since Apple uses a “walled garden” approach to app distribution, deploying apps to your personal iPhone requires registering for a developer account and subsequently creating a code signing and deployment certificate — that process alone is worth its own article. Registration costs $99/year, which you can apply for here. While that cost may seem too high if you’re a hobbyist, that fee also lets you deploy your own apps to the App Store. So if you’ve been thinking seriously about app development you might want to go ahead and at least do it for the first year. Otherwise, feel free to learn as much as you can with the simulator in Xcode before deciding to spend that money.
So what did you do with this sample? Please let us know in the comments for this article.