How to Test Your App on an iOS Device
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.
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'll 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.
On the next page, click the Enroll Now button 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.
2. Create a Certificate Signing Request
After enrolling for the iOS Developer Program, you'll notice that the iOS Dev Center has a slightly different interface.
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 Certificates, Identifiers & Profiles, iTunes Connect, Apple Developer Forums, and the Developer Support Center. In this article, we'll be working in the Certificates, Identifiers & Profiles section. Click the link to the Certificates, Identifiers & Profiles section 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 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 or CSR.
You can create a CSR using OS X's Keychain Access utility, which you can find in the Utilities folder of the 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. 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.
Click Continue, specify a location to save the CSR, and 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.
3. Create a Development Certificate
Head back to the Certificates, Identifiers & Profiles section in the iOS Dev Center. Select the Certificates section in the column labeled iOS Apps.
Click the plus button in the top right and follow the guide for creating a development certificate. In the first section, Development, select iOS App Development, and click Continue.
The next view tells you how to create a CSR. Feel free to click Continue since you've already have a CSR ready to upload.
It's time to upload the CSR we generated earlier. Click the Choose File button at the bottom, select the CSR, and click Generate. It may take a few seconds while the development certificate is being generated.
The certificate should be valid for one year. Click the Download button to download the certificate to your development machine.
Locate the certificate on your development certificate and double-click it to add it to your login keychain.
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 Dev Center.
Browse to the Certificates, Identifiers & Profiles section of the iOS Dev Center, click the Devices tab in the iOS Apps section, and click the plus button in the top right. To register a device, enter a name for the device and he device's UDID. 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 Organizer from the Window menu. Select the Devices tab at the top and select the device you're interested in. The 40 character alphanumeric string next to the label Identifier is the device's UDID.
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.
When you click this button, Xcode will connect to the iOS Dev Center on your behalf. During this process, Xcode will ask you for the credentials of your iOS developer account if you haven't added those to Xcode's Preferences > Accounts. Xcode will then prepare your device for development by downloading the provisioning profiles that contain the device, more about this later.
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 Dev Center for the necessary data behind the scenes. When a device can be used for development, a green indicator is shown on the right of the device's name in Xcode's Organizer.
5. Create an App ID
An app ID is an identifier that uniquely identifies an application on iOS. It's much like the device's UDID that uniquely identifies a device. The app ID is used by the operating system for security reasons and it's an essential component of Apple's Push Notification and iCloud services, among others.
The app ID of an application consists of your application's bundle identifier prefixed with a unique 10 character bundle seed ID generated by Apple. What's 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. You can change the bundle identifier to whatever you like. It's recommended to adopt the reverse-domain naming convention, for example, com.tutsplus.my-first-application. The complete app ID would then be xxxxxxxxxx.com.tutsplus.my-first-application.
To create a new app ID in the iOS Dev Center, navigate to the Certificates, Identifiers & Profiles section, click iOS Apps, and choose App IDs from the menu on the left. To create a new app ID, click the plus button in the top right.
Start by giving your app ID a descriptive name so you can find it later. Leave the app ID prefix field untouched. In the section App ID Suffix, enter your application's bundle identifier. Make sure you enter it in the section labeled Explicit App ID.
You can replace the application name in the bundle identifier by an asterisk, for example, com.tutsplus.*. 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. This type of app ID is a wildcard app ID—as opposed to an explicit app ID.
6. Create a Provisioning Profile
With the development certificate and the app ID in place, it's time to create a provisioning profile for your application. Before we start, it might be useful to explain what a provisioning profile is, 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 the application needs to run on.
It will become clearer if we create a provisioning profile for your application so let's do that now. In the Certificates, Identifiers & Profiles section of the iOS Dev Center, select the Provisioning Profiles tab in the iOS Apps section. Click the plus button in the top right to create a new provisioning profile. Select iOS App Development in the section labeled Development and click Continue.
In the next step, select the app ID you created a few minutes ago and click the Continue button.
Select the development certificate from the list of certificates to associate the new provisioning profile with the correct certificate and click Continue.
You then need to select the devices you wish to link to the provisioning profile. Remember that only these devices will be able to run your application during development.
Give the provisioning profile a descriptive name so you can easily find it later. Click Generate and download the provisioning profile to your development machine. Double-click the provisioning profile to add it to Xcode.
If you wish to add more devices to an already existing provisioning profile, than you can do so by editing the provisioning profile. 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.
7. Configuring the Project
Before you can build and run your application on your device, you need to update the build settings of the target in 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. Select the first item in the list of targets and click the tab labeled Build Settings at the top.
Don't be overwhelmed by the numerous build settings. Scroll through the list 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 iOS Developer. It's usually located under the Automatic heading.
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.
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 the different pieces fit together. It will make debugging issues related to provisioning much easier and I can guarantee you that you'll run into such issues at some point in your iOS development career.