Book Image

iOS 15 Programming for Beginners - Sixth Edition

By : Ahmad Sahar, Craig Clayton
5 (1)
Book Image

iOS 15 Programming for Beginners - Sixth Edition

5 (1)
By: Ahmad Sahar, Craig Clayton

Overview of this book

With almost 2 million apps on the App Store, iOS mobile apps continue to be incredibly popular. Anyone can reach millions of customers around the world by publishing their apps on the App Store. iOS 15 Programming for Beginners is a comprehensive introduction for those who are new to iOS. It covers the entire process of learning the Swift language, writing your own app, and publishing it on the App Store. Complete with hands-on tutorials, projects, and self-assessment questions, this easy-to-follow guide will help you get well-versed with the Swift language to build your apps and introduce exciting new technologies that you can incorporate into your apps. You'll learn how to publish iOS apps and work with Mac Catalyst, SharePlay, SwiftUI, Swift concurrency, and much more. By the end of this iOS development book, you'll have the knowledge and skills to write and publish interesting apps, and more importantly, to use the online resources available to enhance your app development journey.
Table of Contents (32 chapters)
1
Part 1: Swift
10
Part 2: Design
15
Part 3: Code
25
Part 4: Features

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, see 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. Follow these steps:

  1. Use the cable that came with your iOS device to connect your device to your Mac, and make sure the iOS device is unlocked.

    Tip

    You can view connected devices by choosing Window | Devices and Simulators in the Xcode menu bar.

  2. In the Scheme menu, choose your device (iPhone in this case) as the run destination:
Figure 1.13: Xcode Scheme menu with actual iOS device selected

Figure 1.13: Xcode Scheme menu with actual iOS device selected

  1. Wait for Xcode to finish indexing and processing, which will take a while. Once complete, Ready will be displayed in the status window.
  2. 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.:
Figure 1.14: Xcode Signing & Capabilities pane

Figure 1.14: Xcode Signing & Capabilities pane

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 the digital certificate can be generated.

Important Information

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 Information

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: https://help.apple.com/xcode/mac/current/#/dev60b6fbbc7.

  1. Click the Add Account... button:
Figure 1.15: Xcode Signing & Capabilities pane with Add Account... button selected

Figure 1.15: Xcode Signing & Capabilities pane with Add Account... button selected

  1. The Xcode Preferences window appears with the Accounts pane selected. Enter your Apple ID and click Next:
Figure 1.16: Apple ID creation dialog box

Figure 1.16: Apple ID sign in dialog box

Tip

You can also access Xcode preferences by choosing Preferences in the Xcode menu.

  1. Enter your password when prompted. After a few minutes, the Accounts pane will display your account settings:
Figure 1.17: Accounts pane in Xcode preferences

Figure 1.17: Accounts pane in Xcode preferences

  1. Close the Preferences window when you're done by clicking the red Close button in the top-left corner.
  2. In Xcode's Editor area, click Signing & Capabilities. Make sure Automatically manage signing is ticked and Personal Team is selected from the Team pop-up menu:
Figure 1.18: Xcode Signing & Capabilities pane with account set

Figure 1.18: Xcode Signing & Capabilities pane with account set

  1. If you still see errors in this screen, try changing your Bundle Identifier by typing some random characters into it, for example, com.myname4352.ExploringXcode.
  2. 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:
Figure 1.19: Could not launch "ExploringXcode" dialog box

Figure 1.19: Could not launch "ExploringXcode" dialog box

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. Follow these steps:

  1. On your iOS device, tap Settings | General | VPN & Device Management:
Figure 1.20: Device Management setting in iOS Settings

Figure 1.20: Device Management setting in iOS Settings

  1. Tap Apple Development:
Figure 1.21: Apple Development section in Device Management settings

Figure 1.21: Apple Development section in Device Management settings

  1. Tap Trust "Apple Development: ":
Figure 1.22: Trust button

Figure 1.22: Trust button

  1. Tap Trust:
Figure 1.23: Trust dialog box

Figure 1.23: Trust dialog box

  1. You should see the following text, which shows the app is now trusted:
Figure 1.24: Apple Development section with trusted certificate

Figure 1.24: Apple Development section with trusted certificate

  1. 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 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 you'll configure Xcode to connect to your iOS device over Wi-Fi now. Follow these steps:

  1. Make sure your iOS device is plugged into your Mac, and both the Mac and iOS device are on the same wireless network.
  2. Choose Window | Devices and Simulators from the Xcode menu bar:
Figure 1.25: Xcode Window menu with Devices and Simulators selected

Figure 1.25: Xcode Window menu with Devices and Simulators selected

  1. Click on the checkbox marked Connect via network:
Figure 1.26: Xcode Devices and Simulators Window with Connect via network checked

Figure 1.26: Xcode Devices and Simulators Window with Connect via network checked

Awesome! Your iOS device is now connected wirelessly to Xcode, and you no longer need the USB cable to be connected to it.