Using an iOS device for development
Although you'll be able to go through most of the exercises in this book using the simulator, it is recommended to build and test your apps on an actual iOS device, as the simulator will not be able to simulate some hardware components and software APIs.
Important information
For a comprehensive look at all the differences between the simulator and an actual device, refer to this link:
https://help.apple.com/simulator/mac/current/#/devb0244142d
In addition to your device, you'll need an Apple ID or a paid Apple Developer account to build and run your app on your device. You'll use the same Apple ID that you used to download Xcode from the App Store for now:
- Use the cable that came with your iOS device to connect it to your Mac, and make sure it is unlocked.
Tip
You can view connected devices by choosing Window | Devices and Simulators in the Xcode menu bar.
- In the Scheme menu, choose your device (iPhone in this case) as the run destination:
- Wait for Xcode to finish indexing and processing, which will take a while. Once complete, Ready will be displayed in the status window.
- Run the project by clicking the Play button (or use Command + R). You will get the following error: Signing for "Exploring Xcode" requires a development team.:
This is because a digital certificate is required to run the app on an iOS device, and you need to add an Apple ID or paid Apple Developer account to Xcode so that the digital certificate can be generated.
Important note
Using an Apple ID will allow you to test your app on an iOS device, but you will need a paid Apple Developer account to distribute apps on the App Store. You'll learn more about this in Chapter 26, Testing and Submitting Your App to the App Store.
Important note
Certificates ensure that the only apps that run on your device are the ones you authorize. This helps to protect against malware. You can also learn more about them at this link:
- Click the Add Account... button:
- The Xcode Preferences window appears with the Accounts pane selected. Enter your Apple ID and click Next. Note that you can create a different Apple ID if you wish by using the Create Apple ID button:
Tip
You can also access Xcode preferences by choosing Preferences in the Xcode menu.
- Enter your password when prompted. After a few minutes, the Accounts pane will display your account settings:
- Close the Preferences window when you're done by clicking the red button in the top-left corner.
- In Xcode's editing area, click Signing & Capabilities. Make sure Automatically manage signing is ticked and that Personal Team is selected from the Team pop-up menu:
- If you still see errors on this screen, try changing your Bundle Identifier by typing some random characters into it, for example,
com.myname5109.ExploringXcode
. - Everything should work now when you build and run, and your app will be installed on your iOS device. However, it will not launch and you will see the following message:
This means you need to trust the certificate that has been installed on your device. You'll learn how to do this in the next section.
Trusting the Developer App certificate on your iOS device
A Developer App certificate is a special file that gets installed on your iOS device along with your app. Before your app can run, you need to trust it. Let's do that now:
- On your iOS device, tap Settings.
- Tap General.
- Tap Device Management:
- Tap Apple Development:
- Tap Trust "Apple Development:":
- Tap Trust:
- You should see the following text, which shows the app is now trusted:
- Click the Play button in Xcode to build and run again. You'll see your app launch and run on your iOS device.
Congratulations! Note that you have to connect your iOS device to your Mac using the cable in order to build and run your app. You'll learn how to connect to your device over Wi-Fi in the next section.
Connecting an iOS device wirelessly
Unplugging and replugging your iOS device to your Mac gets pretty cumbersome after a while, so now you'll configure Xcode to connect to your iOS device over Wi-Fi. Follow these steps:
- Make sure your iOS device is plugged in to your Mac, and that both the Mac and iOS devices are on the same wireless network.
- Choose Window | Devices and Simulators from the Xcode menu bar:
- Click on the checkbox marked Connect via network:
Awesome! Your iOS device is now connected wirelessly to Xcode, and you no longer need the USB cable to be connected to it.