A Mac app Template in SwiftUI
Notes from a new dev.
I (mostly) wrote a book on writing a Mac app using AppKit. It covered the basics of app development for the Mac. I then abandoned Mac development in favour of learning SwiftUI and creating iOS apps instead. iOS apps are nice and shiny and have all the fancy toys while Mac apps are basic and functional. What would you pick?
Then I had another play with the Mac and, this time, I decided to have a go at creating apps using SwiftUI. I went in to this with the simple though that "it's SwiftUI, how different can it be?". Turns out it has the same 'feel' but the details are very different.
I set about learning what I could and quickly found that the majority of training materials for the Mac deal with AppKit and anything that dealt with SwiftUI was basic and lacking in detail. Some of it is good and some of it seems to stop short of describing the really useful stuff. I followed a couple of tutorials but they barely scratched the surface of things like preferences screens, about box popups and menus.
So, I decided to create a sample Mac app and to document how I created it and to try to go into a little more detail than I had found on the Web. This was the inspiration for this book and the sample project that goes with it.
Key Learning Points
Whats in the book?
I've based the book on the AppKit version to a large extent, but with the content geared towards SwiftUI. Not everything is possible in SwiftUI, so I have dropped back to AppKit only where necessary.
The Default Project
The default project describes the purpose of the document and walks you through creating a basic Mac app. It goes on to explain some of the issues with the default template that Apple give us and proposes some fixes for issues like the default window size, the window title and getting the app to close when the last window is closed.
The dialogs section deals with two items that were glossed over in the tutorials I read; the About Box and the Settings screen. Some tutorials briefly cover settings but I have gone more in-depth, covering multi-tab settings and settings that are not simple check boxes.
The Main Window
The section on the main window takes the standard "Hello World" window to a NavigationStackView, customising the sidebar to set minimum width, add a sidebar toolbar and create a view model to allow the sidebar to communicate with the detail view. We then customise the detail view to add a toolbar, illustrating where icons will be positioned and how it affects the window title.
I next have a short section on refactoring the project. At this point, we have made a lot of changes to the default project that XCode created for us and the code is in need of some cleanup. I work through some structure changes to make the code cleaner and, hopefully, more understandable. I also introduce SwiftLint as a useful tool to enforce coding standards and walk through some basic fixes to our code.
The Menus section was difficult to create. Very few of the tutorials I worked through did more than create basic menus that did nothing useful. Most were menus that printed something to the debug console and did nothing else. In the real world, this information, while useful as a start point, is unrealistically simplified.
I expand upon the usual tutorials to show deleting standard menus, adding new menu items and I provide a mechanism that can connect a menu item to code in the currently active view model. There may be better ways to do this, but what I have gives a clean link between clicking on a menu item and having code run in the view mode of the current view. I have not seen that happen in any of the tutorials I have followed.
For this section, I am having to drop more into AppKit to achieve the display of a file section window and a file save-as window. There are no native components in SwiftUI for displaying these windows, so we have to interface with NSOpenPanel and NSSavePanel. I encapsulate these into a helper struct which should be straight forward to extend in a 'real' application.
More to come
This is a work in progress. At the time of producing this, I am working my way through the File Handling section. I need to decide what else comes next, being mindful not to overload the project with stuff I will probably not need.
Where is it?
Can I have a copy?
The book and it's associated project are available on GitHub in my Mac Template repo. It's going to be under continual development until I decide I have enough functionality built in to the template to make it useful as a starter project. If you just want to take a look at the document itself, it' in the docs folder (or you can click here for it)..
Am I really any good?
Don't take my word for my abilities, take a look at other peoples opinions about me.
One very quickly runs out of superlatives with Steve. He is the cleverest person I know. He is highly innovative, passionate regarding the proper maintenance of agreed standards and protocols and establishes excellent relationships with our clients.
Steve is a rare developer resource who is able to provide a good breakdown of technical solutions for all audiences to understand... Steve understands the importance of project deliverable's both from a client and business perspective and shows a personal pride in ensuring the delivery schedules are met.
I have worked closely with Steve for his time with us and long before we formed our company. His technical ability far out-performs anyone I have known or met in companies we have worked with like IBM/EDS/Cap Gemini
Steven showed a great deal of commitment and skill when he worked with me. His attention to detail, comprehensive documentation and well thought out code, using very impressive technical standards meant that it was always possible to rely on the solutions provided being top notch. Steven is an asset to any company, at any level - if I were able to, I'd love to work with him again.
I have had the pleasure of working with Steven for several years in my role as CTO. He his one to the most multi-disciplined technically skilled developers I have known but what sets him apart is his eye for, and attention to detail, particularly when considering the user experience. I would absolutely not hesitate in recommending Steven.