Provisioning an application for testing on either a physical device or for App Store distribution can be a nightmare for beginners. However, every iOS developer has to tackle this hurdle at some point. In this article, I hope to give you a good understanding of how to properly provision an application for testing on a physical device.
Step 1: iOS Developer Program
As I mentioned in a previous tutorial in this session, if you plan to run development code on a physical device or you intend to publish an application on the App Store, you will first need to enroll for the iOS Developer Program. To do so, open a new browser window and go to the iOS Dev Center. Sign in with your Apple developer account and click the Learn More link in the iOS Developer Program section on the right (figure 1). On the next page, click the Enroll Now button (figure 2) and follow the steps to complete the enrollment.
Keep in mind that it can take several days for your application to be accepted. Apple manually approves each application, which means that you won’t have access to the iOS Developer Program until you get the green light from Apple.
Step 2: Create a Certificate Signing Request
After enrolling for the iOS Developer Program, you will notice that the iOS Dev Center has a slightly different interface (figure 3). In the iOS Developer Program section on the right, you no longer see the Learn More link you clicked to enroll for the program. Instead, you see links to the iOS Provisioning Portal, iTunes Connect, Apple Developer Forums, and the Developer Support Center. In this article, we will be working in the iOS Provisioning Portal. Click the link of the iOS Provisioning Portal on the right.
The provisioning process starts with the creation of an iOS Development Certificate. A certificate is an electronic document that links your digital identity with other information, such as your name, email, and company information. A development certificate consists of a secret private key and a shared public key. If you are familiar with SSL certificates for securing a website, then you probably already know what a certificate is and how it generally works. Xcode uses the private key of the certificate to cryptographically sign your application binary.
To obtain a development certificate, we first need to create a certificate signing request (CSR). You can create a CSR using OS X’s Keychain Access utility. You can find this utility in the Utilities folder in your Applications folder. Open the Keychain Access menu, select Certificate Assistant and choose the option labeled Request a Certificate From a Certificate Authority…. Fill out the form by entering your name and the email address with which you signed up for your Apple developer account (figure 4). Leave the certificate authority email address (CA Email Address) blank and make sure to select the option labeled Saved to disk to save the certificate signing request to your system. Leave the checkbox labeled Let me specify key pair information unchecked (figure 4).
Click Continue and specify a location to save the CSR, and then click Save. Browse to the location you specified to make sure the CSR was generated. In Keychain Access, under the Keys category, you can see that the private and public keys have been added to your login keychain (figure 5).
Step 3: Create a Development Certificate
Head back to the iOS Provisioning Portal and select the Certificates tab on the left. Click the Request Certificate button on the right (figure 6) and upload the CSR we generated in the previous step by clicking the Choose File button at the bottom, selecting the CSR, and clicking the submit button at the bottom of the page (figure 7). You will notice that the status of the new certificate is set to Pending Issuance (figure 8). After refreshing the page once or twice, the status should change to issued and a download button should appear on the right (figure 9). Click the download button to download your brand new iOS development certificate.
Double-click the iOS development certificate to add it to your login keychain (figures 10 and 11).
Step 4: Adding a Device
You cannot run an iOS application on a random device. You need to specify on which devices your iOS application should run by adding one or more iOS devices to the iOS Provisioning Portal. Browse to the iOS Provisioning Portal, click the Devices tab on the left, and click the Add Devices button on the right. To add a device, enter a descriptive device name and enter the device ID (figure 12). The device ID, also known as the UDID, is an identifier that uniquely identifies an iOS device. Note that the UDID is not the same as the device’s serial number.
You can find the UDID of a device by connecting the device with your machine and launching Xcode’s Organizer. You can open the Organizer by selecting Window from the Xcode menu and choosing Organizer. Select the Devices tab at the top and select the device you are interested in. The 40 character alphanumeric string next to the label Identifier is the device ID or UDID.
If you need the UDID of a device and you don’t have Xcode installed, then iTunes is your friend. Connect the device with a Mac or PC that has iTunes installed on it. Launch iTunes and select the device from the list of devices on the left. Under the device’s Summary tab, you should see the device’s serial number. Click the serial number once to reveal the device’s UDID. To copy the UDID sequence to the clipboard, you need to double-click the serial number and then press Command + C. However odd, it seems to be the only way to copy the UDID using iTunes. Jeff LaMarche offers an alternative approach using OS X’s System Information utility. The article is slightly outdated since Jeff refers to the System Profiler instead of the System Information utility.
The first time you connect an iOS device to your Mac and you view the device in Xcode’s Organizer, you should see a button labeled Use for Development (figure 14).
When you click this button, Xcode will ask for your Apple developer credentials as it want to connect to the iOS Provisioning Portal (figure 15). Xcode will then prepare your device for development by downloading all the provisioning profiles that contain the device, more about this later (figure 16). The long and the short of it is that it used to be a pain to prepare devices for development. Xcode has made this much easier by asking the iOS Provisioning Portal for the necessary data behind the scenes.
Step 5: Create an App ID
An app ID is an identifier that uniquely identifies an application in iOS. It is much like the device ID, which uniquely identifies a device. The app ID is used by the operating system for security reasons and it is an essential component of Apple’s Push Notification and iCloud services, among others.
The app ID of an application consists of (1) your application’s bundle identifier prefixed with (2) a unique 10 character bundle seed ID generated by Apple. What is a bundle identifier? Do you remember when you set up your first application? Even though I didn’t cover the bundle identifier in detail, you implicitly specified a bundle identifier for your project by giving your application a name and specifying a company identifier. By default, the bundle identifier is your application’s name prefixed with your project’s company identifier. However, you can change the bundle identifier to whatever you like. It is often recommended to adopt the reverse-domain naming convention, for example, com.mobiletuts.My-First-Application. The complete app ID would then be XXXXXXXXXX.com.mobileTuts.My-First-Application.
For more information about app ID’s, visit the iOS Provisioning Portal, click the tab labeled App IDs on the left, and select the How To tab at the top. To create a new app ID in the iOS Provisioning Portal, click the App IDs tab on the left and click the New App ID button on the right (figure 17). Give the new app ID a descriptive name so you can easily find it later. Specify the bundle seed ID (app ID prefix) and enter your application’s bundle identifier (app ID suffix).
Alternatively, you can replace the application name in the bundle identifier by an asterisk, for example, com.mobileTuts.*. This is useful if you intend to create a suite of applications that need to be able to share keychain access or don’t require keychain access at all. The asterisk or wild-card character needs to be the last component of the bundle identifier.
Step 6: Create a Provisioning Profile
With the development certificate and the app ID in place, it is time to create a provisioning profile for your application. Before we start, it might be useful to explain what a provisioning profile is exactly, because this is something that confuses a lot of new iOS developers. To quote Apple’s documentation, a provisioning profile is a collection of assets that uniquely ties developers and devices to an authorized iOS Development Team and enables a device to be used for testing. In other words, a provisioning profile contains the information that the operating system needs to verify whether an application is permitted to run on a specific device. This implies that the provisioning profile needs to be installed on each device that the application needs to be able to run on.
It will become more clear if we create a provisioning profile for your application, so let’s do just that. In the iOS Provisioning Portal, select the Provisioning tab on the left and click the Development tab at the top. To create a new profile, click the New Profile tab on the right (figure 18).
Start by giving the new provisioning profile a name so you can easily identify it later. As time passes, you will accumulate a fair number of provisioning profiles and it is therefore important to properly name each profile. As I mentioned earlier, each provisioning profile is associated with one or more certificates. Select the development certificate we created earlier. Set the app ID to the app ID we created a few minutes ago and select the devices you intend to run your application on. Submit the form to create the provisioning profile (figure 19).
If you wish to add more devices to an already existing provisioning profile, than you can do so by editing the provisioning profile in the iOS Provisioning Portal. All you need to do is download the new profile and install it on all the devices you wish to test with. In other words, you don’t need to create a new provisioning profile if all you want to do is add or remove devices.
As with the creation of certificates, it takes a few seconds for the new provisioning profile to show up. Refresh the browser window to update the status of the profile and download the provisioning profile to your Mac (figures 20 and 21).
By double-clicking the provisioning profile, Xcode will automatically install the provisioning profile and open the Xcode Organizer (figure 22). Xcode will also install the provisioning profile on any device that you added to the profile if it is connect to your Mac (figure 23).
Step 7: Configuring the Project
Before you can build and run your application on your device, you need to configure the build settings of your Xcode project. Open the Xcode project that you created during the previous tutorial and select the project from the Project Navigator on the left (figure 24). Select the only item in the list of targets and then click the tab labeled Build Settings at the top (figure 24).
Don’t let yourself get overwhelmed by all the build settings. Scroll through the build settings and search for the section titled Code Signing. In this section, search for the subsection titled Code Signing Identity and set the Debug configuration to match your new provisioning profile (figure 25). I hope you understand that it is important to properly name your provisioning profile as things can get messy when you are dealing with a lot provisioning profiles.
Step 8: Build and Run
If you followed the steps correctly, you should now be able to build and run your application on your device. Before running your application, make sure that you properly set the active scheme by selecting your device from the drop down menu (figure 26).
Creating and managing certificates, provisioning profiles, app IDs, and test devices can be a daunting task – and it often is for most developers. I hope that this article has given you a sturdy foundation. Don’t hesitate to read this article a few times to really understand how all the different pieces fit together. It will make debugging issues related to provisioning much easier and I can guarantee you that you will run into such issues!