Becoming a Developer - Learning iPhone Programming (2010)

Learning iPhone Programming (2010)

Chapter 2. Becoming a Developer

Before you start writing code, you need to do some housekeeping. First, you’ll need to install Xcode, Apple’s development environment, as well as the iPhone SDK. Both of these are available directly from Apple, although you may already have Xcode on your Mac OS X install DVD. However, before you can install the iPhone SDK, you’ll have to register with Apple as a developer. If you enroll in one of the developer programs, you’ll also need to create, download, and install a number of certificates and profiles to allow you to deploy your applications onto your iPhone or iPod touch. Let’s get these housekeeping tasks out of the way now so that you can get to the interesting bit—the code—as quickly as you can.

Warning

Developing applications for the iPhone requires an Intel Mac running Mac OS X 10.5 (Leopard) or later.

Registering As an iPhone Developer

Before you can develop for the iPhone, you need to become a registered iPhone developer so that you can download and install the iPhone SDK. This will give you access to the SDK and allow you to build and test your applications in iPhone Simulator.

If you take it a step further and enroll in the iPhone Developer Standard or Enterprise Program (both of these have a yearly fee), you’ll be able to test applications on your own iPhone or iPod touch. We will discuss how to enroll in these programs in the next section.

If you choose the free account, you won’t be able to install your applications onto your own iPhone or iPod touch, nor will you be able to sell applications on Apple’s App Store (Standard Program) or distribute them to people within your own company (Enterprise Program). If you stick with a free account, you also won’t have access to prerelease versions of the iPhone SDK or the iPhone OS.

You can sign up at http://developer.apple.com/iphone/.

Note

If you are an existing Apple Developer Connection (ADC) member, or if you have an iTunes or MobileMe account, you can use your existing Apple ID to register as an iPhone developer. However, if you intend to sell software commercially, you may want to create a new identity for use with the program to keep it separate from your existing Apple ID.

You’ll initially be asked to either choose an existing Apple ID or create a new one. If you create a new one, you’ll be asked for some details (e.g., email and physical addresses); if you choose an existing Apple ID, you’ll still need to confirm some of these details, although they should be filled in with the most recent information Apple has.

You’ll also be asked to provide a professional profile, indicating what sort of applications you’ll be developing and whether you also develop for other mobile platforms.

Finally, you’ll need to agree to the developer license. After you do, a verification code may be sent to the email address you registered with Apple, although this doesn’t happen in all cases. However, if this happens to you, the final step of registering as an iPhone developer will be to verify your email address.

Apple Websites

You’ll use four main websites as part of the iPhone development process:

The iPhone Dev Center

This site is where you can get access to the latest versions of the iPhone SDK, along with background technical information, API documentation, sample code, and instructional videos. You need to be a registered iPhone developer to access the site.

The Developer Program Portal

This site is where you can generate and manage the certificates, provisioning profiles, approved devices, and other housekeeping tasks necessary to test your applications on the iPhone and iPod touch and prepare them for distribution. You’ll need to be both a registered iPhone developer and enrolled in one of the iPhone Developer Programs to access this site.

The App Store Resource Center

This site provides help and advice on how to distribute your application on the App Store, including preparing your app for submission, understanding the App Store approval process, and learning how to manage your apps on the App Store. You’ll need to be both a registered iPhone developer and enrolled in the iPhone Developer Standard Program to access this site.

iTunes Connect

This site provides you with the tools to manage your applications on the iTunes App Store and your contracts with Apple. You’ll need to be both a registered iPhone developer and enrolled in the iPhone Developer Standard Program to access this site.

Enrolling in the iPhone Developer Program

If you intend to sell your applications on the App Store, or you just want to be able to deploy them onto your own iPhone or iPod touch, you’ll also need to enroll in the iPhone Developer Program. If you’ve not already registered as an iPhone developer, you can do that during this process.

Warning

Your iPhone Developer Program membership lasts for 1 year and can be renewed starting 60 days before the expiration date of your existing membership. If you do not renew your membership, your ability to distribute your applications will be curtailed. In addition, your developer and distribution certificates will be revoked. Finally, any applications you have on the iTunes App Store will be removed.

You have two options when enrolling in the iPhone Developer Program. Most people will want to register for the Standard Program, which costs $99 per year. This will allow you to create free—or, once you’ve filled out some paperwork, commercial—applications for the iPhone and iPod touch, and distribute them either via the App Store or via the ad hoc distribution channel where you provide both the application binary and a provisioning certificate to the end user.

Note

Ad hoc distribution allows you to distribute your application directly to your users, bypassing the App Store. However, distribution is limited to just 100 devices during the course of your one-year membership and, at least for the end user, is more complicated than distributing your application via the App Store. It’s mainly intended for beta testing programs, and it isn’t a substitute for publishing your application to the store. If you need to conduct large-scale rollouts to a specific group of users and you want to avoid the App Store, you should probably look at the Enterprise Program.

The more expensive Enterprise Program, at $299, is intended for companies with more than 500 employees that wish to create applications for in-house distribution. While this program allows you to distribute your applications inside your own company, it does not allow you to publish them for sale on the App Store. If you’re thinking about selling your applications to the public, you need the Standard Program.

An iPhone Developer University Program is also available, but this is designed specifically for higher education institutes looking to introduce iPhone development into their curricula. Unless you’re an academic at such an institute, it’s unlikely that this program will be applicable to you.

The Apple Developer Connection

As well as enrolling as a member of the iPhone Developer Program, you may also wish to register as a member of the Apple Developer Connection. Doing so is a good idea if you’re serious about developing with the Mac, and all but the free membership tiers will give you access to the Software Seeding Program, which provides prerelease copies of both the Mac OS X operating system and Apple’s developer tools. You can sign up for ADC at either http://developer.apple.com or https://connect.apple.com.

Three membership plans are available: a free online membership, the $500 Select membership, and the Premier membership that costs several thousand dollars (although it also includes a ticket to the Apple Worldwide Developers Conference).

Installing the iPhone SDK

Once you have registered as an iPhone developer, you can log in to the iPhone Dev Center and download the iPhone SDK.

Note

At the time of this writing, Apple combined the iPhone SDK and Xcode into a single download. It is possible that in future releases you may need to install Xcode first, and then install the iPhone SDK from a separate installer.

Newer prerelease beta versions of the SDK may be available to those enrolled in the iPhone Developer Program; however, the current stable version will be available even if you choose not to pay to enroll in the program.

The combined download of the Xcode development tools and the iPhone SDK is around 2.5 GB in size. The combined bundle will be downloaded as a disk image file. After it downloads, the image should automatically mount; double-click on the iPhone SDK and Tools package file to install the SDK, as shown in Figure 2-1.

Installing the Xcode development tools and the iPhone SDK

Figure 2-1. Installing the Xcode development tools and the iPhone SDK

The installer will ask you to agree to the terms of the software license agreement before prompting you to install the software. You should install it in the suggested location, and the default installation options will include everything you need to develop applications for the iPhone or iPod touch. However, you’ll need at least 5.9 GB of free space on your disk to install the Xcode developer tools and the iPhone SDK.

After installation, you can check that everything has gone OK by starting Xcode, which will have been installed in the /Developer/Application folder on your machine. Project templates should be available for the iPhone OS, as shown in Figure 2-2.

The Xcode New Project window

Figure 2-2. The Xcode New Project window

You now have everything you need to write applications and test them in the simulator.

Warning

If you want to test your code on an actual iPhone, you will need to enroll in either the Standard or Enterprise iPhone Developer Program. However, the amount of time it takes to be accepted into the program varies, so after you enroll and have been accepted, you should bookmark this page and finish the steps in this chapter. You can use iPhone Simulator for the examples in this book while you wait to be accepted.

While the simulator is very good, it’s not perfect. Code runs much faster on the simulator than it does on the device. If you’re dealing with applications that have a complicated UI or consume a great deal of processor power, the difference in performance between the simulator and the device could become important. On several occasions I’ve had to go back and rewrite my code and refactor the way in which my UI functions; when I tested my application on the simulator it worked fine, but on real hardware it just ran too slowly. You can also allocate much more memory in the simulator than is available on the real hardware.

Additionally, some frameworks are available to you in the simulator—notably the NSPredicate and NSXMLDocument classes—that just don’t exist on the device. Code that uses these missing classes will compile and run on the simulator, but not on the device. As well as regularly building your application in iPhone Simulator, it’s therefore a good idea to do regular device builds. If you accidentally use one of these “missing” classes, it will show up as a link error at compile time for such a build. After all, you don’t want to get too far down the road of developing your application only to discover (hours, or worse yet, days later) that you’re using classes or frameworks that aren’t actually present on the device.

Note

Both NSPredicate and NSXMLDocument are commonly used classes. For instance, NSXMLDocument is the class most people programming in Objective-C on the Mac (rather than the iPhone) would use to perform an XQuery on an XML document. The lack ofNSXMLDocument is something that most developers notice quite quickly.

While I’ve seen some complaints that the simulator can sometimes be slightly off on pixel alignment of UIKit elements, I’ve not yet come across this myself. However, when using lower-level graphics libraries, such as OpenGL ES, the renderer used on the iPhone and iPod touch is slightly different from the one used in the simulator, so when a scene is displayed on the simulator it may not be identical to the actual device at the pixel level.

Additionally, the simulator has some built-in limitations. For instance, if your application’s UI is designed to respond to touch events with more than two fingers, you can’t test it in the simulator.

Note

While it doesn’t allow you to simulate gestures requiring many fingers, iPhone Simulator does allow you to test applications that require two-finger (multitouch) touch gestures. You can use Option-click (for pinch) or Option-Shift-click (for drag) while using the mouse to get two “fingers.”

Furthermore, you will not have access to the accelerometer, GPS, Bluetooth, or digital compass when running your application in the simulator. If your application relies on these hardware features, you have no choice but to test it on your device.

Preparing Your iPhone or iPod touch

Before you can install applications onto your iPhone or iPod touch, you must follow a number of steps, and you’ll need to do so in the order shown in Figure 2-3.

The workflow for creating certificates and mobile provisioning profiles

Figure 2-3. The workflow for creating certificates and mobile provisioning profiles

So, if you have enrolled in either the Standard or Enterprise iPhone Developer Program, now is the time to generate the appropriate certificates and provisioning profiles so that you will be able to deploy the test application from the next chapter onto your device.

Certificates and Provisioning Profiles

You must have a development certificate for Xcode to sign your application binaries. This certificate also identifies you as a developer. When you build your iPhone application, Xcode will look in your Mac OS X keychain for this certificate and the corresponding certificate from Apple, called the WWDR Intermediate certificate, which you’ll also need to download from the Developer Portal.

Provisioning profiles associate a development certificate, and hence a developer, with a hardware device and an iPhone application ID, which is a unique identifier for your application. To install an application that you’ve signed with your development certificate onto your iPhone or iPod touch, you need to install the associated provisioning profile onto your device.

Creating a Development Certificate

The first thing you need is a development certificate and Apple’s WWDR Intermediate certificate. To request a development certificate from the Developer Portal, you need to generate a certificate-signing request (CSR) using the Keychain Access application.

You can find the Keychain Access application in the /Applications/Utilities folder. Launch the application and select Keychain Access→Preferences from the menu. Go to the Certificates Preferences pane to confirm that the Online Certificate Status Protocol (OCSP) and Certificate Revocation List (CRL) options are turned off, as shown in Figure 2-4.

The Certificates tab of the Keychain Access application’s Preferences pane

Figure 2-4. The Certificates tab of the Keychain Access application’s Preferences pane

Next, select Certificate Assistant→Request a Certificate from a Certificate Authority from the Keychain Access menu, and enter the email address that you selected as your Apple ID during the sign-up process along with your name, as shown in Figure 2-5. Click the “Saved to disk” and the “Let me specify key pair information” radio buttons and then click Continue. You’ll be prompted for a filename for your certificate request.

The Keychain Access.app Certificate Assistant

Figure 2-5. The Keychain Access.app Certificate Assistant

Accept the defaults (a key size of 2,048 bits using the RSA algorithm) and click Continue. The application will proceed to generate a CSR file and save it to disk. The file will be saved to the location you specified when you were prompted to choose a filename (the default is usually your desktop).

Next, log in to the iPhone Dev Center and click on the link to the iPhone Developer Program Portal. This will take you to the main portal used to manage certificates and devices associated with your developer program account.

Click the Certificates link, then go to the Development tab and click Request Certificate. Follow the instructions to upload your CSR file to the portal.

If you joined the iPhone Developer Program as an individual, you now need to approve your own certificate request, again in the Development tab in the Certificates section of the portal (simply click Approve). If you are part of a development team, your nominated team admin must do this for you.

Note

If you don’t see the Download option appear after you click Approve, click the Development tab link to refresh the page, and it should appear.

Once you do this, you need to download your personal certificate and the WWDR Intermediate certificate and install them in your Mac OS X keychain.

Still in the Development tab, click the Download button to download your personal certificate. Next, right-click on the link to the WWDR Intermediate certificate and save the linked file to disk.

Once both of these certificates have downloaded to your local machine, you need to install them in your Mac OS X keychain. Double-click on the certificate files to install them into your keychain. This will activate the Keychain Access application and ask you to confirm that you want to add the certificates to your Mac OS X keychain.

Warning

If you have more than one keychain, you need to make sure the certificates are installed in the default keychain, normally called login. The default keychain is highlighted in bold in the list of keychains at the top left of the Keychain Access.app application. It’s normally best to keep the login keychain the default, but if this is not the case you can make it the default by selecting the File→Make Keychain “login” Default option from the menu bar. If the certificates are not installed into the default keychain, Xcode will be unable to find them, and hence will be unable to sign binaries with them. This means you will not be able to install your applications onto your iPhone or iPod touch.

You can check that the two certificates have been correctly installed in your keychain by clicking on the Certificates category in the Keychain Access application. You should see both your own developer certificate and Apple’s WWDR certificate in the login keychain, as shown in Figure 2-6.

The Keychain Access application showing the newly installed certificates necessary for Xcode to sign your binaries and deploy them onto your iPhone

Figure 2-6. The Keychain Access application showing the newly installed certificates necessary for Xcode to sign your binaries and deploy them onto your iPhone

Getting the UDID of Your Development Device

Plug the iPhone or iPod touch you intend to use for development into your Mac. Open Xcode and select the Window→Organizer item from the menu bar. The Organizer window will open, showing the list of connected devices (see Figure 2-7).

The Xcode Organizer window

Figure 2-7. The Xcode Organizer window

You’ll need the unique device identifier (UDID) of your development device so that you can create a mobile provisioning profile for this device. Right-click or Ctrl-click on the 40-character string labeled Identifier (see Figure 2-7) and select Copy.

Return to the iPhone Developer Program Portal, click Devices, and select the Manage tab. Next, click Add Devices. Enter the device name in the appropriate box and the UDID in the box labeled Device ID, and click Submit. You have now registered your device.

Creating an App ID

To install your application onto your iPhone or iPod touch, you will need to create an App ID. This is a unique identifier that the iPhone uses to grant your application access to its section of the keychain, limiting the application’s access to usernames, passwords, and certificates used by other applications.

The App ID is also used as part of the mobile provisioning profile. The mobile provisioning profile is different from the certificate you generated earlier. Certificates stay in the keychain on your Mac and are used by Xcode to digitally sign the iPhone application binaries. The mobile provisioning profile you’re about to generate is tied to one or more devices and is transferred by Xcode onto your iPhone or iPod touch. This allows the applications you create to run on that device.

Go to the App IDs section of the iPhone Developer Program Portal, select the Manage tab, and click on New App ID. Enter a name for your App ID; this should be a human-readable name used to refer to this particular App ID. It’s entirely arbitrary what you use as the name for the App ID. Since this is your first App ID, for the Bundle Seed ID select Generate New.

Finally, enter a Bundle Identifier. This must be unique, and most developers use a reversed version of their domain name so that this is the case. For instance, my domain name is babilim.co.uk, so I entered uk.co.babilim.* as my Bundle Identifier.

Note

The asterisk (*) that appears at the end of my Bundle Identifier is the wildcard symbol. Using a * in the Bundle Identifier means you will be able to use this App ID for multiple applications. If you did not use a wildcard here, you’d have to generate a new App ID for each of your applications, and a new provisioning profile for each of these applications, before you could deploy your application onto your iPhone or iPod touch. Using a wildcard means you can generate a single mobile provisioning profile that will allow you to deploy multiple applications onto your developer device.

Technically, this means that all the applications created using this Bundle Identifier will share the same portion of the keychain on your iPhone. I discuss the implications of this later. Using a wildcard in the Bundle Identifier also means that the applications you create using this App ID, and the mobile provisioning profile(s) associated with it, will not be able to use the Apple Push Notification and in-app purchase services.

Click Submit. The portal will now generate a new 10-character Bundle Seed ID and prepend it to the Bundle Identifier you provided. This is your App ID.

You need to make a note of your Bundle Identifier as you’ll need to supply it to Xcode, as described near the end of Chapter 3, to allow you to deploy the application you are developing onto your iPhone or iPod touch.

Creating a Mobile Provisioning Profile

Now you’re ready to create a mobile provisioning profile. Go to the Provisioning section of the iPhone Developer Program Portal, select the Development tab, and click on New Profile.

Enter a profile name. While it’s more or less arbitrary what you put here, I recommend using “Developer Profile” somewhere in the name. You may be generating a number of provisioning profiles, including ones later on for distribution (both ad hoc and to the App Store), so it’s helpful to know that this profile is to be used for development.

Check the relevant certificate box: if you’re an independent developer, you’ll have only one choice here, the certificate you generated earlier using the Keychain Access application.

Select the App ID you generated in the previous section, and then select the development device (or devices if you have more than one available) for which this profile will be valid. As I mentioned before, Xcode will transfer the provisioning profile onto your iPhone or iPod touch, and application binaries built by Xcode using a provisioning profile will run successfully only on devices for which this profile is valid. If you don’t select the correct device here, your code will not run on it. Don’t worry, though: you can add additional devices to the profile at any time, but you’ll need to regenerate a provisioning profile inside the Program Portal.

Click Submit to generate the new mobile provisioning profile that you’ll use during development. I discuss provisioning profiles needed for distributing your applications later in the book. The status will appear as pending; click the Development tab to reload it until it is no longer pending.

When the profile is ready, click Download and download the provisioning profile to your Mac. You can install it in a number of ways, but the easiest way is to drag the .mobileprovision file you downloaded onto the Xcode icon in the dock. This will install it in Xcode and make it available for development.

Making Your Device Available for Development

The final step before you can start coding is to make your device available for development. Return to Xcode and click Window→Organizer from the menu. Select your development device from the lefthand pane and click Use for Development. If Xcode doesn’t manage to correctly register your device, you may have to disconnect and reconnect your iPhone or iPod touch so that Xcode can find it correctly. If that fails to work, you should try turning your device off and then on again. Depending on the version of the SDK you installed and the version of the OS currently on your device, you may have to restore your device from the Organizer window inside Xcode. In the process, you’ll lose any data you have on it. If this is necessary, you can back up your data by syncing with iTunes as normal before restoring the OS using Xcode. After the restore, return to iTunes and restore your data.

If you can afford the extra cost, I recommend using a separate device for development than you use as your day-to-day iPod or phone. In the future, you may wish to install prerelease versions of the iPhone operating system onto your development device, and by definition, these are always unstable. If you’re relying on your iPhone to keep you in touch, you may not want to use it for development.

Once you’ve installed the profiles, you can verify that Xcode has correctly stored them by opening the Library folder in your home directory and looking in MobileDevice/Provisioning Profiles. The next time you sync your development device with iTunes (and you should probably do that now), the mobile provisioning profile will be installed onto it.

You can verify that the profile has been installed by going to Settings→General→Profile on your iPhone and iPod touch and checking that the profile has been correctly installed and verified, as shown in Figure 2-8.

You can now confirm that everything has worked correctly by noting the status light next to your device in the Xcode Organizer window. If Xcode has managed to connect to the device, and it is correctly enabled for development, the status light next to the listing on the lefthand pane will be green. You’ll also see your mobile provisioning profile listed in the center box in the main pane, as shown in Figure 2-9.

The development provisioning profile installed on my iPod touch

Figure 2-8. The development provisioning profile installed on my iPod touch

The Xcode Organizer window showing my iPod touch ready for development with my development provisioning profile installed

Figure 2-9. The Xcode Organizer window showing my iPod touch ready for development with my development provisioning profile installed

Congratulations, you now have all the certificates and profiles in place to allow you to start running code on your iPhone or iPod touch.