Explore 1.5M+ audiobooks & ebooks free for days

Only $12.99 CAD/month after trial. Cancel anytime.

iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
Ebook1,502 pages5 hours

iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success

Rating: 0 out of 5 stars

()

Read preview
LanguageEnglish
PublisherPackt Publishing
Release dateDec 9, 2024
ISBN9781836204886
iOS 18 Programming for Beginners: Learn iOS development with Swift 6, Xcode 16, and iOS 18 - your path to App Store success
Author

Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.

Related to iOS 18 Programming for Beginners

Related ebooks

Operating Systems For You

View More

Reviews for iOS 18 Programming for Beginners

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    iOS 18 Programming for Beginners - Ahmad Sahar

    Preface

    Welcome to iOS 18 Programming for Beginners. This book is the ninth edition of the iOS Programming for Beginners series, and has been fully updated for iOS 18, macOS 15.0 Sequioa, and Xcode 16.

    In this book, you will build a journal app called JRNL. You will start off by exploring Xcode, Apple’s programming environment, also known as its Integrated Development Environment (IDE). Next, you will start learning the foundations of Swift, the programming language used in iOS apps, and see how it is used to accomplish common programming tasks.

    Once you have a solid foundation of using Swift, you will start creating the user interface of the JRNL app. During this process, you will work with storyboards and connect your app’s scenes together using segues.

    With your user interface complete, you will then add code to implement your app’s functionality. To start, you’ll learn how to display data using a table view. Next, you’ll learn how to add data to your app, and how to pass data between view controllers. After that, you’ll learn how to determine your device location and display annotations on a map. You’ll then learn how to persist app data using JSON files create custom views, and add photos from the camera or photo library. Finally, you’ll make your app work on devices with larger screens, such as an iPad or Mac, by implementing a collection view in place of a table view.

    You now have a complete app, but how about adding the latest iOS 18 features? You’ll start by learning about SwiftData, which allows you to describe data models and manipulate model instances using regular Swift code. Next, you will learn how to develop apps using SwiftUI, a great new way of developing apps for all Apple platforms. After that, you’ll learn how to test your code using Swift Testing, and how to bring Apple Intelligence features into your apps.

    Finally, you’ll learn how to test your app with internal and external testers and get it into the App Store.

    Who this book is for

    This book is tailored for individuals with minimal coding experience who are new to the world of Swift and iOS app development. A basic understanding of programming concepts is recommended.

    What this book covers

    Chapter 1, Exploring Xcode, takes you through a tour of Xcode and talks about all the different parts that you will use throughout the book.

    Chapter 2, Simple Values and Types, deals with how values and types are implemented by the Swift language.

    Chapter 3, Conditionals and Optionals, shows how

    if

    and

    switch

    statements are implemented, and how to implement variables that may or may not have a value.

    Chapter 4, Range Operators and Loops, shows how to work with ranges and the different ways loops are implemented in Swift.

    Chapter 5, Collection Types, covers the common collection types, which are arrays, dictionaries, and sets.

    Chapter 6, Functions and Closures, covers how you can group instructions together using functions and closures.

    Chapter 7, Classes, Structures, and Enumerations, talks about how complex objects containing state and behavior are represented in Swift.

    Chapter 8, Protocols, Extensions, and Error Handling, talks about creating protocols that complex data types can adopt, extending the capabilities of existing types, and how to handle errors in your code.

    Chapter 9, Swift Concurrency, introduces you to the concepts of parallel and asynchronous programming, and shows you how you can implement them in your app.

    Chapter 10, Setting Up the User Interface, deals with creating the JRNL app and setting up the initial screen the users will see.

    Chapter 11, Building Your User Interface, covers setting up the main screen for the JRNL app.

    Chapter 12, Finishing Up Your User Interface, covers setting up the remaining screens for the JRNL app.

    Chapter 13, Modifying App Screens, is about configuring each screen of the app in a storyboard.

    Chapter 14, Getting Started with MVC and Table Views, covers working with a table view and how you can use it to display a list of items.

    Chapter 15, Getting Data into Table Views, concerns the incorporation of data into table views using an array as a data source.

    Chapter 16, Passing Data between View Controllers, teaches you how to add data entered using a view controller to an array, and how to pass data from the array to another view controller.

    Chapter 17, Getting Started with Core Location and MapKit, deals with working with Core Location and MapKit to determine your device location and add annotations to a map.

    Chapter 18, Getting Started with JSON Files, involves learning how to store and retrieve user data using a JSON file.

    Chapter 19, Getting Started with Custom Views, teaches you how to create and use a custom view that displays a star rating.

    Chapter 20, Getting Started with the Camera and Photo Library, talks about how to get photos from your camera or photo library into your app.

    Chapter 21, Getting Started with Search, teaches you how to implement a search bar for your main screen.

    Chapter 22, Getting Started with Collection Views, shows you how to implement collection views in place of table views to suit devices with larger screens, such as a Mac or iPad.

    Chapter 23, Getting Started with SwiftData, deals with implementing Apple’s new SwiftData framework to persist data on your app.

    Chapter 24, Getting Started with SwiftUI, introduces building an app using Apple’s new SwiftUI technology.

    Chapter 25, Getting Started with Swift Testing, teaches you how to test your code using Swift Testing.

    Chapter 26, Getting Started with Apple Intelligence, shows you how to add Apple Intelligence features to your app.

    Chapter 27, Testing and Submitting Your App to the App Store, concerns how to test and submit your apps to the App Store.

    To get the most out of this book

    This book has been completely revised for iOS 18, macOS 15.0 Sequioa, Xcode 16, and Swift 6. Part 4 of this book also covers the latest technologies introduced by Apple during WWDC 2024, which are SwiftData, SwiftUI, Swift Testing, and Apple Intelligence.

    To complete all the exercises in this book, you will need:

    A Mac computer running macOS 14.0 Sonoma, macOS 15.0 Sequioa, or later

    Xcode 16.0 or later

    To check if your Mac supports macOS 15.0 Sequioa, see this link: https://www.apple.com/my/macos/macos-sequoia-preview/. If your Mac is supported, you can update macOS using Software Update in System Preferences.

    To get the latest version of Xcode, you can download it from the Apple App Store. Most of the exercises can be completed without an Apple Developer account and use the iOS Simulator. If you wish to test the app you are developing on an actual iOS device, you will need a free or paid Apple Developer account.

    The following chapter requires a paid Apple Developer account: Chapter 27, Testing and Submitting Your App to the App Store. Instructions on how to get a paid Apple Developer account are included.

    Download the example code files

    You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/iOS-18-Programming-for-Beginners-Ninth-Edition. If there’s an update to the code, it will be updated in the GitHub repository.

    We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Code in Action

    Visit the following link to check out videos of the code being run:

    https://www.youtube.com/playlist?list=PLeLcvrwLe185EJSoURfHhSHfbPFkiZl6m

    Download the color images

    We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781836204893.

    Conventions used

    There are a number of text conventions used throughout this book.

    CodeInText

    : Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "So, this is a very simple function, named

    serviceCharge()

    ."

    A block of code is set as follows:

    class

    ClassName

    { property1 property2 property3 method1() { code } method2() { code } }

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    let

    cat

    =

    Animal

    ()

    cat.

    name

    =

    "

    Cat"

    cat.

    sound

    =

    Mew

    cat.

    numberOfLegs

    =

    4

    cat.

    breathesOxygen

    =

    true

    print

    (cat.

    name

    )

    Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Launch Xcode and click Create a new Xcode project:"

    Important notes

    appear like this.

    Tips

    appear like this.

    Get in touch

    Feedback from our readers is always welcome.

    General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

    Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

    Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

    If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

    Leave a review!

    Thank you for purchasing this book from Packt Publishing—we hope you enjoy it! Your feedback is invaluable and helps us improve and grow. Once you’ve completed reading it, please take a moment to leave an Amazon review; it will only take a minute, but it makes a big difference for readers like you.

    https://packt.link/r/1836204892

    Scan the QR code below or visit the link to receive a free ebook of your choice.

    A qr code with a square in the middle Description automatically generated

    https://packt.link/NzOWQ

    Download a free PDF copy of this book

    Thanks for purchasing this book!

    Do you like to read on the go but are unable to carry your print books everywhere?

    Is your eBook purchase not compatible with the device of your choice?

    Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

    Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

    The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.

    Follow these simple steps to get the benefits:

    Scan the QR code or visit the link below:

    https://packt.link/free-ebook/9781836204893

    Submit your proof of purchase.

    That’s it! We’ll send your free PDF and other benefits to your email directly.

    Part 1

    Swift

    Welcome to Part 1 of this book. In this part, you will begin by exploring Xcode, Apple’s programming environment, which is also known as the Integrated Development Environment (IDE). After that, you will start learning the foundations of Swift 6, the programming language used in iOS apps, and see how it is used to accomplish common programming tasks.

    This part comprises the following chapters:

    Chapter 1, Exploring Xcode

    Chapter 2, Simple Values and Types

    Chapter 3, Conditionals and Optionals

    Chapter 4, Range Operators and Loops

    Chapter 5, Collection Types

    Chapter 6, Functions and Closures

    Chapter 7, Classes, Structures, and Enumerations

    Chapter 8, Protocols, Extensions, and Error Handling

    Chapter 9, Swift Concurrency

    By the end of this part, you’ll understand the process of creating an app and running it on Simulator or a device, and you’ll have a working knowledge of how to use the Swift programming language in order to accomplish common programming tasks. This will prepare you for the next chapter and will also enable you to create your own Swift programs. Let’s get started!

    1

    Exploring Xcode

    Welcome to iOS 18 Programming for Beginners. I hope you will find this a useful introduction to creating and publishing iOS 18 apps on the App Store.

    In this chapter, you’ll download and install Xcode on your Mac. Then, you’ll explore the Xcode user interface. After that, you’ll create your first iOS app and run it in Simulator. Finally, you’ll run your app on an iOS device.

    By the end of this chapter, you will know how to create an iOS app, how to run it in Simulator, and how to run it on an iOS device.

    The following topics will be covered in this chapter:

    Downloading and installing Xcode from the App Store

    Exploring the Xcode user interface

    Running your app in Simulator

    Running your app on an iOS device

    Technical requirements

    To do the exercises for this chapter, you will need the following:

    An Apple Mac computer (Apple Silicon or Intel) running macOS 14 Sonoma or macOS 15 Sequoia

    An Apple Account (if you don’t have one, you will create one in this chapter)

    Optionally, an iOS device running iOS 18

    The Xcode project for this chapter is in the

    Chapter01

    folder of the code bundle for this book, which can be downloaded here:

    https://github.com/PacktPublishing/iOS-18-Programming-for-Beginners-Ninth-Edition

    Check out the following video to see the code in action:

    https://youtu.be/g3mNosIoR8E

    You’ll start by downloading Xcode, Apple’s integrated development environment (IDE) for developing iOS apps from the App Store, in the next section.

    The total size of the download is very large (2.98 GB for Xcode and 8.36 GB for iOS 18 Simulator), so it may take a while to download. Ensure that you have enough disk space prior to downloading.

    Downloading and installing Xcode from the App Store

    Xcode is Apple’s IDE for developing macOS, iOS, iPadOS, watchOS, tvOS, and visionOS apps. You’ll need to download and install Xcode on your Mac prior to writing your first app. Follow these steps:

    On your Mac, choose App Store from the Apple menu.

    In the search field in the top-right corner, type

    Xcode

    and press the Return key.

    You’ll see Xcode in the search results. Click Get and then click Install.

    If you have an Apple Account, type it in the text field and enter your password when prompted. If you don’t have one, click the Create Apple Account button and follow the step-by-step instructions to create one:

    Figure 1.1: Apple account creation dialog box

    You can see more information on how to create an Apple Account using this link: https://support.apple.com/en-us/108647#appstore.

    Once Xcode has been installed, launch it. You’ll see a license agreement screen. Click Agree:

    Figure 1.2: License agreement screen

    You’ll be prompted to enter your Mac’s administrator username and password. Once you have done so, click OK:

    Figure 1.3: Prompt for administrator username and password

    You’ll see a screen showing you the available development platforms. You just need macOS and iOS for now. Tick iOS 18.0, leave all other options unticked, and click Download & Install:

    Figure 1.4: Development platforms screen

    Xcode will prompt you to relaunch to use updated frameworks. Click Relaunch Xcode:

    Figure 1.5: Relaunch Xcode prompt

    You’ll see a What’s New in Xcode screen. Click Continue:

    f

    Figure 1.6: What’s New in Xcode screen

    You’ll see the Welcome to Xcode screen. Click Create New Project... in the left-hand pane:

    Figure 1.7: Welcome to Xcode screen

    Xcode will start to download iOS 18.0 Simulator automatically. Note that you will not be able to run any apps on Simulator until this process has been completed:

    Figure 1.8: Simulator download progress bar

    You’ll see the new project screen as follows. In the Choose a template for your new project section, select iOS. Then choose App and click Next:

    Figure 1.9: New project screen

    You’ll see the Choose options for your new project screen:

    Figure 1.10: Choose options for your new project screen

    Configure the options as follows:

    Product Name: The name of your app. Enter

    JRNL

    in the text field.

    Organization Identifier: Used to create a unique identifier for your app on the App Store. Enter

    com.myname

    for now. This is known as the reverse domain name notation format and is commonly used by iOS developers.

    Interface: The method used to create the user interface for your app. Set this to Storyboard.

    Testing System: The testing system you will use. You will learn about this in Chapter 25,Getting Started with Swift Testing. Set it to None for now.

    Leave the other settings at their default values. Click Next when done.

    You’ll see a Save dialog box. Choose a location to save your project, such as the Desktop or Documents folder, and click Create:

    Figure 1.11: Save dialog box

    You’ll see a dialog box saying Git Repository Creation Failed. Click Fix.

    The reason why you see this dialog box is because the Source Control checkbox in the Save dialog box was ticked. Apple recommends that Source Control be turned on. Source Control is outside the scope of this book but if you wish to learn more about version control and Git, see this link: https://git-scm.com/video/what-is-version-control.

    You will see the Source Control screen as follows:

    Figure 1.12: Source Control preference screen

    Enter the following information:

    Author Name: Your own name

    Author Email: Your email address

    Close the Source Control screen by clicking the close button in the top-left corner when done. The Xcode main window will appear.

    Fantastic! You have now successfully downloaded and installed Xcode and created your first project. In the next section, you will learn about the Xcode user interface.

    Exploring the Xcode user interface

    You’ve just created your first Xcode project! As you can see, the Xcode user interface is divided into several distinct parts, as shown here:

    Figure 1.13: Xcode user interface

    Let’s look at each part in more detail. The following description corresponds to the numbers shown in the preceding screenshot:

    Toolbar (1): Used to build and run your apps, and view the progress of running tasks.

    Navigator area (2): Provides quick access to the various parts of your project. The Project navigator is displayed by default.

    Editor area (3): Allows you to edit source code, user interfaces, and other resources.

    Inspector area (4): Allows you to view and edit information about items selected in the Navigator area or Editor area.

    Debug area (5) – Contains the debug bar, the variables view, and the Console. The Debug area is toggled by pressing Shift + Command + Y.

    Next, let’s examine the toolbar more closely. The left side of the toolbar is shown here:

    Figure 1.14: Xcode toolbar (left side)

    Let’s look at each part in more detail. The following descriptions correspond to the numbers shown in the preceding screenshot:

    Navigator button (1) – Used to display and hide the Navigator area.

    Stop button (2) – Only appears next to the Run button when the app is running. Stops the currently running app.

    Run button (3) – Used to build and run your

    Enjoying the preview?
    Page 1 of 1