How to Build your First iPhone App

The Easiest Way to Build your First iPhone App

Mobile websites have come a long way, but if you want to take full advantage of a smartphone’s hardware, or get listed in the iTunes App Store, you need to compile your code. In this tutorial, we’ll show you how to create a mobile web app with an iPhone look and feel using jQTouch, then turn it into a native iPhone app using Phonegap. No Objective-C necessary.

Tutorial Details

  • Program: Phonegap
  • Version: 0.80
  • Difficulty: Intermediate
  • Estimated Completion Time: 1 hour

Requirements

To complete this tutorial, you’ll need the following:

Introduction to PhoneGap

PhoneGap is an open-source framework that can turn any web app into a native app for iPhone, BlackBerry and Android. It pulls off
this trick by running your web code in a UIWebView, an embedded instance of Safari without the
toolbars and buttons of the standalone Safari app. PhoneGap then extends this basic functionality by mapping features of the
iPhone SDK to JavaScript objects you can call in your web code, so you can easily add features like GPS, camera, contacts, vibration,
SQLLite and accelerometer support. And when you’re ready to distribute your app, PhoneGap 0.80 is Apple-approved!

Included in the PhoneGap distribution is everything you need to build and run an iPhone app. The included XCode project is bundled
with a sample code showing how to use many of the native features, and all the supporting files necessary to compile the app and
run it in the iPhone Simulator or on your phone.

Building and Running an iPhone App

To test whether you’ve got your Mac ready to run your code, let’s try out the sample project included with PhoneGap.

First, open up the iPhone folder, and double-click on PhoneGap.xcodeproj:

This should open XCode with your project loaded. Although there’s a lot going on here, we as web developers only need
to concern ourselves with the www folder. This contains the web code that will become the interface and logic of
our app.

Now that we’ve got our project loaded, it’s time to take it for a spin. Bundled with the iPhone SDK is an iPhone Simulator that
hooks right in to XCode. All we have to do is click “Build and Run.”

Building Your Web App

For the sake of this tutorial, I’ve put together a simple, native-feeling app that displays my Tumblr feed with a slide-up “About”
screen. This app is based on the excellent jQTouch framework, a jQuery-based library of UI
elements, animations, and extensions that let you rapidly develop mobile web apps with native look and feel. Let’s take a quick look
at putting together a web app using jQTouch before we import this app into our Phonegap project.

First, we load jQuery, jQTouch, and some bundled theme files in the <head> tag; these will style our elements to look like
native iPhone UI widgets:

	<head>
		<script src="jqtouch/jquery.1.3.2.min.js" type="application/javascript" charset="utf-8"></script>
		<script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>
	    <style type="text/css" media="screen">@import "jqtouch/jqtouch.min.css";</style>
	    <style type="text/css" media="screen">@import "jqtouch/themes/apple/theme.min.css";</style>
	    <style type="text/css" media="screen">@import "master.css";</style>

		<script type="text/javascript">
	        $.jQTouch();
	    </script>
	</head>

Then we build out the skeleton of our app:

	<body id="stage" class="theme">
        <div id="home" class="current">

        </div>
        <div id="about">

        </div>
    </body>

jQTouch takes any direct descendent of the <body> tag and converts it into a full-screen “view”. This means every
<div> in the code above will take over the entire screen, and changing screens means swapping between <div>s by linking
to them by their id:

	<a href="#about">About</a>

JQTouch includes a variety of cool ways to transition between these screens, and they and can be enabled simply by adding CSS classes.
For example, to turn that link to the About page into a slide-up transition, we add the following:

	<a class="slideup" href="#about">About</a>

Then, in the About page itself, we add a button to “close” the panel by sliding it back:

	<a href="#" class="grayButton goback">Close</a>

On the default screen, we’ll add a toolbar with the aforementioned “About” button, and a spot to embed a Tumblr feed:

    <div class="toolbar">
        <h1>Home</h1>
        <a class="button slideup" href="#about">About</a>
    </div>
    <h2>Live Stream</h2>
    <div id="timeline">
        <script type="text/javascript" src="http://YOUR_TUMBLR_USERNAME.tumblr.com/js">
        </script>
    </div>

Lastly, a few CSS classes that will style the output of the Tumblr feed to match our “Apple” theme:

	ol {
	    color: black;
	    background: #fff;
	    border: 1px solid #B4B4B4;
	    font: bold 17px Helvetica;
	    padding: 0;
	    margin: 15px 10px 17px 10px;
	    -webkit-border-radius: 8px;
	}

	ol > li {
	    color: #666;
	    border-top: 1px solid #B4B4B4;
	    list-style-type: none;
	    padding: 10px 25px 10px 25px;
	}

That’s it! After adding some content to our About page, we replace the files in our Phonegap project’s www folder
with our new web app, and run it again:

Conclusion

Our web app is now compiled, and from here can be packaged up for distribution in the iTunes Store. It’s a simple start, but
in very little time we’ve got a native app that looks like Apple’s own, runs on any iPhone, and can be extended to a variety of uses.

I’ll be covering how to extend your app with support for cameras and geo-location in future tutorials. In the meantime, you can
read more about Phonegap at the Phonegap Wiki. Documentation is not fully fleshed out,
so you may find yourself digging through git repositories after the end of a long hunt.

To submit your app to the iTunes App Store, register for the iPhone Developer Program.
Once you’re registered, you’ll be given the tools to digitally sign and submit your app to the iTunes Store.

Write a Plus Tutorial

Did you know that you can earn up to $600 for writing a PLUS tutorial and/or screencast for us? We’re looking for in depth and well-written tutorials on HTML, CSS, PHP, and JavaScript. If you’re of the ability, please contact Jeffrey at nettuts@tutsplus.com.

Please note that actual compensation will be dependent upon the quality of the final tutorial and screencast.

Write a PLUS tutorial

Add Comment

Discussion 60 Comments

  1. David Moreen says:

    I have been waiting so long for Nettuts to do some iPhone stuff. I read a book on making iphone apps last summer, it was fun and somewhat simple, but the thing is, I forgot just about all that I learned. This is a good refresher.

  2. Ash Smith says:

    Anyone know of software for windows… or should i fork out an buy a mac?

  3. This is a valuable tut.
    I will give it a try INSHALLAH

  4. depi says:

    What about http://www.appcelerator.com/ ? It is also open source project and I think it has more features then Phonegap, what do you think about it? I would appreciate tutorials for that “framework” as well.

  5. hello
    Ok now i can build iphone app, but Can I sell it after that
    thanks

  6. fractalbit says:

    And please, also consider an android tutorial!!!

  7. johnny says:

    @اسعار الذهب اليوم: You can sell it. Some html/js/css driven apps are already in the store. Probably apple will reject it…

    I don’t think it’s worth to code all that in html/css/js.
    You can’t even use all frameworks. Just get some time to learn cocoa. :)

  8. adrian says:

    I found it useful to change the color of the statusbar based on the design, the easiest way to do so is to open up the info.plist file and add key UIStatusBarStyle and set its value to one of
    UIStatusBarBlack
    UIStatusBarBlackTranslucent
    UIStatusBarTranslucent
    to change it to the default
    UIStatusBarDefault

  9. I wouldn’t call this an iPhone tutorial. Its basic markup and JS. The only thing iPhone related is that its ran within an iPhone.

  10. Jack says:

    Excellent stuff! I’ve been having a look at making iPhone/iPod apps today, and had in fact just downloaded the SDK when this popped up in mail! It’s currently 1:15 AM where I am now, so I’ll check this out tomorrow, but it looks fantastic. Will phonegap work with the iPad? If not, do you know of any plans to add such functionality?

    Keep up the splendid (yes, I’m English) work!

  11. Marc says:

    I used jqtouch and phonegap to write an app that searches the PubMed database with 16 million medical articles.

    Apple accepted it.

    You can learn more here:

    http://mobileabstracts.com

    http://itunes.apple.com/us/app/mobile-abstracts/id348575624?mt=8&uo=6

  12. Nice tutorial. I didn’t know about PhoneGap. Appcelerator looks awesome too.

  13. Wow !

    That’s great !

    Thanks !

  14. Andrew says:

    Bill! Woo!

  15. illastr8 says:

    Exactly what I was looking for. Thank you for posting this! Good stuff!!!

    Illastr8

  16. Latavish says:

    This was an excellent example! An Andriod tutorial would be wounderful as well.

  17. Clemente says:

    Niiiice, All these new Programs and Compilers are keeping us from actually learning Obj C. I think we are all going to have to learn it eventually though. Nice Article!!

  18. John says:

    Silly me, I though I’d cliked on Net tuts, not Mac App Storm….

  19. Stever says:

    Anyway you could upload your completed code?

    Also… how would one compile the app into an .IPA file for testing on an actual phone?

    • mikemick says:

      Completed code would be appreciated. The tut didn’t quite spell out exactly what the markup should look like. My app is just opening up Safari on the iPhone Simulator (obviously not working). I’m not sure if it is my HTML markup or what.

  20. Politics says:

    Good topics for you. Phone Gap is important Relay for fast working.

  21. jeanv says:

    very nice tuto… but i might be silly, but do you compil the code, to transform “simple” html code into native app ?

  22. Is there any application that uses this method and that I can download from the store

  23. jones says:

    Would you like some fun games and humour, check out this new iphone app called iSICKO
    http://bit.ly/986BGN

  24. Apple themselves also supply some tutorials and good documentation found when you’re logged in to the iPhone dev center; another possibility is development using HTML, CSS & javascript – tjek out PhoneGap.

  25. Fredricz says:

    Is this tutorials not possible to try in windows?

  26. Elving says:

    This is awesome, but would you be able to access core iphone functions? Or is it just a web app running as an iphone app?

  27. Dale says:

    Love this and would love to see a lot more iPhone tuts, possible a channel for iPhone.tutsplus.com

  28. Chad says:

    You can do most of the example on a Win/Linux box using javascript/jQuery/jQTouch and displaying on safari but if you want to deploy to iPhone or use the accelerometer/camera/contacts/gps/etc, you will need to do use phoneGap on the Mac. Johnathon Stark has a book out on this (oReilly).

  29. X says:

    Safe to do this on a jailbroken iPhone?

  30. Floris says:

    Is apple now blocking these pre-made template/framework things?

  31. Yeah Floris, Apple has decided to block everything which is not *originally* written in Objective-C, C or C++
    The problem is you see, any gaping/bridging etc. technology drastically decreases the performance and the capabilities of your app, if you are really serious about developing a stunning app you need to go the Objective C way

    Besides the fact this practice is not banned from the App store, the tutorial is quite well written, enjoyed

  32. andrew says:

    Check out http://www.BiznessApps.com – its an easy DIY iPhone app company for small businesses. They offer iPhone apps for only $39 a month.

  33. Thank you for this tut! I came here while searching the web for good iPad programming tuts for starters.

  34. Robin Nixon says:

    If you’ll forgive the plug there’s also my new book, HTML5 for iOS and Android, which enables you to take web apps created in HTML, JavaScript & CSS, and turn them into standalone apps that you can upload to the app stores (for free or to sell). See http://html5formobile.com – the wrappers to do this for the iOS and Android SDKs are freely available on the website, and you don’t need any knowledge of either programming language if you follow the instructions in the book.

  35. singh says:

    Check this out – http://iosappdevelopment.blogspot.com/2011/07/best-ways-to-find-out-great-iphone-app.html, to know best ways to find out best-matching iPhone app developer for you.

  36. I have to agree with Miles comment, this is indeed basic markup and JS.Still udeful though none the less..

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.