Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
iOS 14 Programming for Beginners

You're reading from   iOS 14 Programming for Beginners Get started with building iOS apps with Swift 5.3 and Xcode 12

Arrow left icon
Product type Paperback
Published in Nov 2020
Publisher Packt
ISBN-13 9781800209749
Length 766 pages
Edition 5th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ahmad Sahar Ahmad Sahar
Author Profile Icon Ahmad Sahar
Ahmad Sahar
Arrow right icon
View More author details
Toc

Table of Contents (31) Chapters Close

Preface 1. Section 1: Swift
2. Chapter 1: Getting Familiar with Xcode FREE CHAPTER 3. Chapter 2: Simple Values and Types 4. Chapter 3: Conditionals and Optionals 5. Chapter 4: Range Operators and Loops 6. Chapter 5: Collection Types 7. Chapter 6: Functions and Closures 8. Chapter 7: Classes, Structures, and Enumerations 9. Chapter 8: Protocols, Extensions, and Error Handling 10. Section 2:Design
11. Chapter 9: Setting Up the User Interface 12. Chapter 10: Building Your User Interface 13. Chapter 11: Finishing Up Your User Interface 14. Chapter 12: Modifying and Configuring Cells 15. Section 3:Code
16. Chapter 13: Getting Started with MVC and Collection Views 17. Chapter 14: Getting Data into Collection Views 18. Chapter 16: Getting Started with MapKit 19. Chapter 17: Getting Started with JSON Files 20. Chapter 18: Displaying Data in a Static Table View 21. Chapter 19: Getting Started with Custom UIControls 22. Chapter 20: Getting Started with Cameras and Photo Libraries 23. Chapter 21: Understanding Core Data 24. Section 4:Features
25. Chapter 22: Getting Started with Apple Silicon Macs 26. Chapter 23: Getting Started with SwiftUI 27. Chapter 24: Getting Started with Widgets 28. Chapter 25: Getting Started with App Clips 29. Chapter 26: Testing and Submitting Your App to the App Store 30. Other Books You May Enjoy

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:

  1. 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.

  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

  3. Wait for Xcode to finish indexing and processing, which will take a while. Once complete, Ready will be displayed in the status window.
  4. 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 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:

    https://help.apple.com/xcode/mac/current/#/dev60b6fbbc7

  5. 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

  6. 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:
    Figure 1.16 – Apple ID creation dialog box

    Figure 1.16 – Apple ID creation dialog box

    Tip

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

  7. 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

  8. Close the Preferences window when you're done by clicking the red button in the top-left corner.
  9. 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:
    Figure 1.18 – Xcode Signing & Capabilities pane with account set

    Figure 1.18 – Xcode Signing & Capabilities pane with account set

  10. 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.
  11. 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. Let's do that now:

  1. On your iOS device, tap Settings.
  2. Tap General.
  3. Tap Device Management:
    Figure 1.20 – Device Management setting in iOS Settings

    Figure 1.20 – Device Management setting in iOS Settings

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

    Figure 1.21 – Apple Development section in Device Management settings

  5. Tap Trust "Apple Development:":
    Figure 1.22 – Trust button

    Figure 1.22 – Trust button

  6. Tap Trust:
    Figure 1.23 – Trust dialog box

    Figure 1.23 – Trust dialog box

  7. 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

  8. 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:

  1. 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.
  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

  3. 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.

You have been reading a chapter from
iOS 14 Programming for Beginners - Fifth Edition
Published in: Nov 2020
Publisher: Packt
ISBN-13: 9781800209749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image