Three macOS development techniques I wish I knew earlier
I’ve been working on my first macOS app using SwiftUI very recently and there are so many unknown unknowns. Here are a few things that are extremely simple and straightforward yet I wish I knew earlier.
Settings screen
Every app includes a settings screen, serving as the central hub where users can customise their experience. Creating the settings screen in SwiftUI is a straightforward process, similar to designing any other screen. However, the challenge lay in determining how to effectively present the settings screen and integrate it with the shortcut key and macOS menu bar.
If you have a SettingsView for your settings screen, you can seamlessly incorporate it into your app within the XXXApp file.
struct SettingsView: View {
var body: some View {
// Your settings content here
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
Settings {
SettingsView() // ⚙️ Here's your settings view
}
}
}
That’s all it takes to set up your settings screen.
You can now open Settings via menu bar or shortcut ⌘ ,. If you want to open settings screen programatically, for instance — via a button tap, you can use SettingsLink as follow.
SettingsLink {
Label("Settings", systemImage: "gear") // 👈 Your custom view here
}
About screen
If your app doesn’t already have an About screen, macOS generates one automatically for you. Don’t believe it? Just create a new macOS project, run it, and click on your app name next to the Apple logo in the top left corner. Then select “About [Your App Name]” — you’ll see your app’s icon along with its version number.
If you want to customise this screen, you can add text, such as “2024 SuperCool Developer Limited.” To do this, create a new Rich Text File (RTF) and name it “Credits.rtf.” RTF files allow for basic formatting options, which you can easily modify using the toolbar in your text editor.
When you run your app again, macOS will automatically search for the Credits.rtf file in your app bundle and display it on the About screen. That’s all it takes to enhance your app with a polished About section!
Hide unnecessary menu bar items
The menu bar items represent macOS’s native method for interacting with your app’s features. However, I often find them unnecessary for my applications, likely due to my beginner status and the simplicity of my current projects. If you are an experienced developer creating applications for power users, you may wish to skip this section.
In the realm of SwiftUI, these menu items are referred to as commands. From here on out, I will use the term “commands” to describe them. SwiftUI offers several built-in command groups to streamline your development process. By simply replacing them with empty commands, you can effectively hide these items from the macOS menu bar.
WindowGroup {
ContentView()
}
.commands {
CommandGroup(replacing: .newItem) { }
CommandGroup(replacing: .undoRedo) { }
CommandGroup(replacing: .pasteboard) { }
}
This will remove the New, Undo/Redo, and Cut/Copy/Paste menu items entirely, leaving no replacement options available. If your app later supports these actions, you can reintroduce them at that time.
Additionally, I wanted to remove the “Show Tab Bar” option under the “View” menu, which I find unnecessary for most of my app’s functionality. To hide this option, you can include an AppKit code in your view’s onAppear method.
YourView()
.onAppear {
NSWindow.allowsAutomaticWindowTabbing = false // 👈 hiding "Show Tab Bar" menu
}