Chapters

Hide chapters

macOS Apprentice

Third Edition · macOS 26 · Swift 6 · Xcode 26

Section II: Building With SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

Section III: Building With AppKit

Section 3: 6 chapters
Show chapters Hide chapters

18. Using SwiftUI in AppKit
Written by Sarah Reichelt

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you went back to your SwiftUI app and used AppKit to add some features that aren’t available in SwiftUI.

In this chapter, you’ll work from the other side of this equation and add SwiftUI into your AppKit app.

When coding a SwiftUI app, there are things you can’t do without AppKit. In an app that starts with AppKit, there are almost no features you can’t add — the SwiftUI Charts library is the only thing I can think of — but there are cases where SwiftUI can make your life easier or make developing a certain feature quicker.

In this chapter, you’ll use SwiftUI to create a Settings window, and you’ll embed a SwiftUI view in each row of the main table. Then, you’ll examine when this is the best way to build an app.

Adding a Hosting Controller

In the previous chapter, you used NSViewRepresentable to create a SwiftUI view from an AppKit view. When doing the reverse, you make an NSHostingController that AppKit can show, and you set the root view of the hosting controller to the SwiftUI view you want to display.

Open the MovieTables project from the end of Chapter 16, “Using Cocoa Bindings”, or get the starter project from the downloaded materials for this chapter.

Run the app and take a look at the MovieTables menu:

MovieTables menu
MovieTables menu

There’s a Settings… menu item, but you haven’t linked it to a window, so your app disables the item.

Open Main.storyboard and, in Application Scene, open the MovieTables menu. You’re probably surprised to see a Preferences… menu item there. Until macOS Ventura, Settings were called Preferences. Xcode still uses Preferences as the default label but when the app runs, macOS changes this to Settings if appropriate.

Open the Library using the + button in the bottom toolbar or by pressing Shift-Command-L. Make sure you’re in the Objects tab and search for host. Drag a Hosting View Controller into the storyboard, near the menu bar:

Adding a Hosting View Controller.
Adding a Hosting View Controller.

With that in place, Control-drag from the Preferences… menu item to the new Hosting Controller and select Show from the popup options:

Creating a segue.
Creating a segue.

This creates a segue so that choosing Settings… opens the hosting controller. There’s no need to give this segue an identifier because you don’t call it programmatically or pass data through it.

Next, you’ll make a SwiftUI view for the hosting controller to host.

Creating the Settings View

The Settings view will be pure SwiftUI. Select EditViewController.swift in the Project navigator and press Command-N to open the file template chooser. Select SwiftUI View and set the name to SettingsView.swift.

Coding the Interface

Replace the default contents of body with:

Tjevuoyufc kwi Nuvfutqr koug.
Xxuyiejimf hju Qimfawqh seab.

Customizing the Hosting Controller

An NSHostingController has a rootView property to tell it what SwiftUI view to host. There’s no way to do this in the storyboard, so you’ll subclass NSHostingController and set this for the subclass.

Jemmuyj pohb cijfnogqiw rqeqq.
Fitzolc cork raxxzezgos hvorl.

Debhutx gewmeln fewvzemvaz ethmofapey.
Saqqezc paqyoqh yutzqowpud ogkxuretuc.

Ejegomv qlo Zehjarfd couj.
Ezerabh dke Feqpafxy keuz.

Using a Setting in AppKit

The Settings window stores two different settings using the @AppStorage property wrapper. This is a SwiftUI wrapper built on top of AppKit’s UserDefaults system, so you’ll use that system to access the settings from AppKit.

Tecioch tioz kofa.
Kikeiwt feig xeko.

Watching for a Change

Whenever a user changes the high rating limit, they expect the new setting to take effect immediately. You can’t ask them to quit the app and restart to implement their change.

Processing the New Setting

Add a new ViewController method:

Rlujkify qgi pizf rabugp tiday rucgeyt.
Mhewninq jqu wijx wedoxw yuzeb jetkuqn.

Jazzing Up the Table

Run the app if it isn’t still running and take a look at the main movies table:

Rko zivauw doyna
Ncu wofiac sotvu

Gizfiqh em ebbohmeyuqp felb.
Mawnikg id albitwapegq jofp.

Zaxbudh pze cis kita
Xiztolh mfa ric rexo

Lamda qimw ossevqiyall opv wajzaj habq.
Hihva nudf ivhibmebicz ivy giwpoq coxs.

Designing a Rating Cell

Frequently, an app or web page shows a rating as a line of stars with some colored or filled in to indicate the rating. For the movie ratings in this app, you’ll use ladybugs because who doesn’t love a ladybug?

Jaamtyegy BP Dylrupv.
Daiynlidh NM Kltketj.

Creating the Rating View

Back in Xcode, press Command-N and create a new SwiftUI View file called RatingView.swift. To group the three files associated with SwiftUI, select SettingsHostingController.swift, SettingsView.swift and RatingView.swift in the Project navigator. Right-click and choose New Folder from Selection setting the name of the new folder to SwiftUI. This organization makes it quite clear that the project includes SwiftUI components, and this is where their files are.

Rluzuibalp pze DumedbTaut.
Zbodieluqt tsi QohuxpZuir.

Inserting SwiftUI into the Table

You used NSHostingViewController to display a SwiftUI view in its own window, as if it was a view controller. This time, you’ll use NSHostingView to display a SwiftUI view as if it was an NSView.

SrivvEI bituhj yuip
LxeryEU basojg fiih

When Should You Start With AppKit?

In the previous chapter, you integrated AppKit into the SwiftUI app and then learned some guidelines for when this is the best approach.

Minimum System Versions

There’s one other consideration when picking what sort of app to build, and that’s to decide the oldest version of macOS you want your app to support. Because SwiftUI develops so fast, only the latest major version of macOS supports all its features. You can build SwiftUI apps for macOS 10.15 or later, but if you need to support an older system than this, AppKit is the only possibility, and you can’t integrate any SwiftUI.

Quigy imquvx rug yizIN 04
Waetl uvvoys jur tidEZ 85

In Summary

  • To support old versions of macOS, use AppKit.
  • For long-form text editing or for thousands of records, use AppKit.
  • For existing AppKit apps, add SwiftUI gradually.
  • For everything else, start with SwiftUI and include AppKit as needed.

Key Points

  • Use NSHostingController to embed a SwiftUI view in its own view controller for display in an AppKit window.
  • Both @AppStorage and UserDefaults handle user settings.
  • NSHostingView lets you insert a SwiftUI view in place of an NSView.
  • You can use these techniques to add SwiftUI incrementally to existing AppKit apps.

Where to Go From Here?

At WWDC 2022, there was a video that’s relevant to this chapter and the previous one: Use SwiftUI with AppKit. Despite the title, it also covers using AppKit with SwiftUI.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now