macOS by Tutorials
by Sarah Reichelt
Edition 3.0, December 2024
Copyright © 2024 Sarah Reichelt.
Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner.
Notice of Liability
This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of their own respective owners.
Attribution
The author originally created portions of this work for the benefit of Kodeco, a community of developers who love to share their knowledge with the world. www.kodeco.com
License
By purchasing macOS by Tutorials, you have the following license:
-
You are allowed to use and/or modify the source code in macOS by Tutorials in as many apps as you want, with no attribution required.
-
You are allowed to use and/or modify all art, images and designs that are included in macOS by Tutorials in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from macOS by Tutorials.
-
The source code included in macOS by Tutorials is for your personal use only. You are NOT allowed to distribute or sell the source code in macOS by Tutorials without prior authorization.
-
This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies.
All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
All trademarks and registered trademarks appearing in this guide are the properties of their respective owners.
Preface
Welcome to the third edition of macOS by Tutorials.
The first edition of this book was published in April 2022 and a lot has changed in the Swift and SwiftUI world since then. macOS itself has also seen a lot of changes with the release of macOS Ventura, Sonoma, and now Sequoia, and there have been lots of improvements to Xcode.
In 2023, Kodeco (formerly raywenderlich.com) changed their approach from being similar to a library to becoming more like a school or college. Sadly, this meant that they were no longer going to publish books like this one. However, since it is now out-of-catalog, they have allowed me to take over the book and publish in my own name. They have since reversed their stance on publishing, so you can expect to see more Kodeco books in the future.
I’d like to acknowledge their assistance and pay tribute to the editors who worked with me on the original edition: Richard Critz, Audrey Tam and Ehab Amer. Special mention to Manda Frederick who was the book manager for the first edition and without whom, this book would never have been written. Also a big thank you to Matt Derrick, former CEO of Kodeco, who agreed to and arranged the transfer of this book to me.
The major changes in this edition include:
-
Updated projects to include new features introduced in macOS 15 Sequoia and Xcode 16.
-
More granular chapter organization. The table of contents is now more of an index so you can find the sections you need, with each chapter being more focussed on a single topic.
-
All projects use Xcode’s folder structure.
-
Tabs use the new syntax.
-
Previews use the new
@Previewable
macro. -
Tracking the active window uses the
@Entry
macro. -
The app in section 4 demonstrates the new windowing options.
I hope you enjoy this updated edition of macOS by Tutorials.
About the Author
I got hooked onto trying to make computers do what I told them a very long time ago and have never stopped loving it. I’m a keen evangelist for developing native Mac apps. When not at my computer, I love coffee, puzzles, reading and cooking — the day hasn’t started until the first cup of coffee is drunk and the crossword is done! — Sarah
About the Language Engineer (Editor)
I’m a fan of words and the things you can do with them, and I’ve spent a decade floating between different forms of writing — academic, fiction, technical. I hope that in this book I’ve succeeded at easing the passage of ideas between Sarah’s brain and your own, and I ask only that you blame me for any typos, stray capitals, Australianisms, and unoxfordenised commas that have made it into the final manuscript. — Peter
Dedication
To Tim who is endlessly supportive, even when listening to me complain about all my bugs.
Introduction
What do I love about programming for the Mac?
I use three Apple devices every day. My iPhone is primarily for communication; my iPad is mostly for entertainment. But the device where I spend most of my time is my Mac. The Mac is the most powerful, flexible and unrestricted device Apple makes, and I love using it.
When writing Mac apps, I get to do so many things that iOS apps cannot do, are not allowed to do, or are not suited to. I can make beautiful, intricate, powerful apps that I use every day.
I use and write iOS apps, too. There is definitely a place for both, but I feel sad that so many developers don’t even consider the enormous possibilities of the Mac app development world.
I’m so happy that you’ve decided to consider those possibilities and join me on this journey!
What You Need
To follow along with this book, you’ll need the following:
-
A Mac computer with an Intel or M series processor, running macOS 15 or later. Any Mac that you’ve bought in the last few years will do, even a Mac mini or MacBook Air.
-
Xcode 16 or later. Xcode is the main development environment for building macOS Apps. It includes the Swift compiler, the debugger and other development tools you’ll need. You can download the latest version of Xcode for free from the Mac App Store.
-
A basic understanding of Swift and SwiftUI. If you’re new to Swift, you might want to read my macOS Apprentice book first: https://www.kodeco.com/books/macos-apprentice
Where to download the materials for this book
The source code for this book can be cloned or downloaded from the GitHub repository:
Click the link to open it in your browser. You’ll see a big green Code button. Click on that and on Download ZIP. This puts all the code files in your Downloads folder.
Depending on your browser settings, you may need to double-click the ZIP file to expand it. That gives you a folder containing one sub-folder for each chapter. The text of each chapter tells you when you need to use any of these files or folders, but every chapter that involves coding has a final folder showing the end result of working through that chapter.
How to read this book
The chapters in each section are designed to take you from start to finish building a particular kind of app. While the book is fun from the first page to the last, if one section especially piques your interest, you’re free to dive right in there. If you are looking for guidance with a particular aspect of Mac development, scan the table of contents. The new, shorter chapter structure makes it easier to find what you need.
When you get to the code blocks, if the code is unfamiliar to you, then I strongly recommend that you type it in for yourself, rather than copy and paste it. Xcode’s autosuggestions will help you type, and having to get every detail correct will embed the new knowledge in your brain in a way that reading it can’t.
If you intend to use copy and paste for any of the code blocks, then go to Xcode’s Settings and in Text Editing → Indentation, turn on Re-Indent on paste. This will solve a lot of issues with the format of the text in the book differing from Xcode’s format.
This book is available as HTML, PDF or ePub. I recommend reading this book from the HTML version. It has good light and dark variations and the most accurate formatting. Also, the code blocks have a COPY button for when you want it, and there are no issues with pagination. My next favorite is PDF — use the light or dark version to suit your preference. My least favorite option is the ePub, as its display depends greatly on your ePub reader.
If you find any errors or typos, please report them to me at [email protected]. For problems with the code, please open an issue at https://github.com/trozware/mos_book_code/issues.
This book is split into five sections:
Section I: Your First App: On This Day
In chapters 1-14, you’ll begin your journey developing for macOS by building a full-featured app using SwiftUI. The app, On This Day, accesses a public network API to collect information about events, births and deaths for a given date. Along the way, you’ll learn how to manage multiple windows, add menu and toolbar commands, and choose multiple display options. You’ll experience first-hand the power of SwiftUI and see just how easy it is to build an app that has all of the look and feel you expect in a macOS app.
Section II: Building a Menu Bar App
In chapters 15-27, you’ll use AppKit to build a Pomodoro-style time tracking app that lives only in the macOS menu bar. Along the way, you’ll learn how to manage timers, update the menu in real-time, and integrate a SwiftUI view into an AppKit app. You’ll also learn about how macOS “sandboxes” apps to protect both them and the system itself.
Section III: Building a Document-based App
In chapters 28-34, you’ll return to using SwiftUI and explore how to build a document-based app. You’ll create a Markdown editor — there can never be enough Markdown editors in the world! — that allows you to preview your text in real time. Along the way, you’ll add menu commands to change the styling of the preview and add formatting to your Markdown text.
Section IV: Advanced Wizardry
Because macOS has its roots in Unix, it provides a vast array of command line tools which allow power users to perform tasks ranging from system management to image manipulation. In chapters 35-43, you’ll learn how to build a graphical front-end for one such command: sips. Once you’ve built your sips GUI, you’ll enable automation to allow your new command to appear in the Services menu and Shortcuts app. When you complete this section, you too will be a wizard!
Section V: Distributing Your macOS Apps
Once you’ve written your app, you’ll want to distribute it to others so they can benefit from your creativity. On macOS, you have more distribution options than you do on iOS. In chapters 44-45, you’ll explore the pros and cons of those options so you can choose which is best for you.
Note
|
The Swift team released Swift 6 in 2024. The main focus of this update is strict concurrency checking, which can lead to some interesting errors and warnings. Since the default for new Xcode projects is still Swift 5, I have decided not to switch to Swift 6 yet. I think that the team needs another year to improve this and make it more usable. |
Section I: Your First App: On This Day
Begin your journey developing for macOS by building a full-featured app using SwiftUI. The app, On This Day, accesses a public network API to collect information about events, births and deaths for a given date. Along the way, you’ll learn how to manage multiple windows, add menu and toolbar commands and choose multiple display options. You’ll experience first-hand the power of SwiftUI and see just how easy it is to build an app that has all of the look and feel you expect in a macOS app.
If you haven’t already downloaded it, you can clone or download the source code and assets from the GitHub repository: https://github.com/trozware/mos_book_code
Chapter 1: Creating the Data Models
In this section, you’ll build a SwiftUI app called On This Day which pulls notable events for a day from an API and displays them in various ways. The app uses an interface style that appears in many macOS apps, with a navigation sidebar, details area, toolbar, menus, and settings window. You’ll end up with an app that looks like this:
When starting a new app, it’s tempting to jump right into the interface design, but this time you’ll start by working out the data models. There are two reasons for this. First, in a SwiftUI app, the data drives the display, so it makes sense to work out the data structure before you start laying out the interface. Secondly, this data comes from an external source and may not have the structure you’d like. Spending some time now to analyze and parse will save you a lot of time and effort later on.
In this chapter, you’ll use a playground to fetch the data, analyze the structure, and create the data models for the app.
Data model design is a vital first step in the development of any app, so working through this chapter will be a valuable experience, but if you’re already familiar with downloading data, parsing JSON, and creating data structs and classes, feel free to skip ahead. In Chapter 2: Setting Up the Project, you’ll download and import the data model files used in this section and start building the user interface.
Where is the Data Coming From?
You’ll use the API from ZenQuotes.io. Go to today.zenquotes.io in your browser and look around the page. At the top, you’ll see an interesting historical event that happened on this day of the year, and you can scroll down to see more: