Building to Your Device
As fun and exciting as it is to use the simulator, eventually, you want to put your code on a real device. It’s a two-step process to set up the device and create signing certificates. But then, you can install your app on the device to test it and show it off.
Without joining the Apple Developer Program, you can install your code on a few devices you have physical access to. Joining the Apple Developer Program allows you to install on more devices for testing but only registered ones. Having your app accepted for sale in the Apple App Store is the only way to get your code installed on an unlimited number of devices.
Setting Developer Mode
For devices running iOS 14 and below, get started by launching Xcode. Then, connect your device using a USB or a USB-C cable and open the Devices and Simulators window. A dialog will appear on the phone asking if you trust this computer. Trusting the computer and entering the passcode on the device will pair it with Xcode and make it ready to accept builds.
Starting with iOS 15, Apple changed the process. Because running arbitrary code on a device could be a security risk, Apple wanted to make sure you really want to lower the security on your phone. They introduced a new setting in the Privacy & Security section of the Settings app called Developer Mode. This new setting isn’t visible until you attach the device to a computer running Xcode. Then, Xcode will give you instructions for where to find the setting.
Now, navigate to Privacy & Security settings on the phone and scroll to the very bottom to see the Developer Mode option. After toggling on Developer Mode, you’ll need to restart the device and then confirm, one last time, that you want to turn Developer Mode on. Turning Developer Mode off also requires a restart but won’t hide the option.
Once you enable Developer Mode, the large, white, blank space in the Devices and Simulators window shows more information. As with the simulators, the iOS version and model appear. However, while for simulators, screenshots and opening the device console are managed in the Simulator app, for devices, they’re managed from this window.
Below the list of installed apps are + and - buttons for installing and removing .ipa executables. You can also drag and drop an .ipa executable to this window to install. The circle button next to them is for accessing the app’s container. With a simulator, you can navigate to the container and the other files, but on an actual device, it’s impossible to snoop around.
Device Conditions lets you simulate poor network and thermal conditions to test how your app responds to these. For example, even when the phone connects to WiFi, you could make the network operate at 3G speeds. Newer phones also allow access to the GPU to throttle its performance.
Creating Provisioning Profiles
Next, you’ll create a Provisioning Profile. This file links your personal Apple ID, or your team’s Apple ID once you join the Apple Developer Program, to your app’s bundle ID and your devices’ IDs. Xcode uses the provisioning profile to digitally sign the .ipa executables to help make sure that only the code you want to runs.
If someone were to modify your .ipa to hack into it, the signature and the provisioning profile would keep the modified .ipa from running on your devices and prevent the hacker from running the .ipa on their devices.
First, sign in with an Apple ID in Xcode. You can add a new account in the Settings window in the Accounts section.
You choose what kind of account after clicking the plus button to add one. To build to a device, you add your Apple ID. However, this is also where you add credentials for a version control server. You use that to enable Xcode’s integrated version control features.
Then, you enter your Apple ID. This is the same ID you use to access iCloud and other Apple services. This isn’t necessarily the same as the credentials for your local computer. If you have two-factor authentication or other security measures enabled, you’ll need to enter those in addition to your email and password.
After a successful sign-in, Xcode generates a new team for your personal use. If you were a member of an Apple Developer Program team, it would also appear in this list.
Back in the Signing & Capabilities screen for the target, you can change the drop-down from None to the team’s name. Xcode will generate the certificates to use for this app.
Once Xcode creates the certificate, it’ll appear in Xcode. There are no errors or warnings at this point, and you’re almost ready to build to the device.
As stated earlier, the certificates tie together the app’s bundle identifier with your Apple ID and any device ID. The certificate itself is stored in the Keychain Access app. Certificates do have expiration dates, but Xcode is pretty good at helping you renew them.
When certificates expire, Xcode doesn’t automatically delete them, so you’ll have expired certificates in Keychain Access over time. Sometimes, this causes issues as Xcode will try to use the expired one, not a renewed one. Whenever you get build errors and Xcode starts complaining about certificates, one of the first places to check is to see if Keychain Access has expired certificates.
Building to Your Device
Now, in Xcode, you can select your attached device as a build destination using the menu at the top of the window. When you click the build and run button, Xcode will build and install your code for the device.
Xcode will ask for permission to access the certificate in Keychain Access. This time, you enter the credentials for a user on this computer with administrator rights. This isn’t necessarily the same as your Apple ID.
Then, after all of that, you’ll likely see error messages, not a running app, on your device.
Apple wants to be doubly, extra sure that you want to install and run this code on your device. You’ll need to trust yourself one last time.
In the Settings app on your phone or iPad, in the General section, scroll to the bottom to see the VPN & Device Management section. On this screen, you’ll see all of the developer certificates that this device trusts. The first time you install a developer build from a new developer, you’ll have to trust them. The trust stays active until you’ve deleted all of that developer’s apps from your device.
Tapping the Trust button completes the process. Importantly, this requires a network connection because the device will check with Apple that all certificates are valid.
Now that this app is trusted, you can launch it by tapping the icon on your device or clicking the build and run button again in Xcode.
Never has Hello World looked so great!