Categories
Apple MacOS App Swift SwiftUI

Practical SwiftUI stuff I learned while building a Photo Viewer for MacOS

Photo Viewer for Mac
Photo Viewer for MacOS made using SwiftUI

If you just want to tinker with all the code, you can get it here.

How to make your SwiftUI MacOS app open a file

You can open the file by using “Command + Click” Open With dialog, or you change the file association to use your app as the default app for that file type.

Since my MacOS app is used to open images, the code is for opening image types:

VStack{
//.....
}.onOpenURL(){ url in
      if let imageData = try? Data(contentsOf: url) {
         DispatchQueue.main.async {
         image = NSImage(data: imageData)
        }
    }
}

Don’t forget to remove the App Sandbox in Xcode so you can access the files in the operating system. Unfortunately this will make your app very unlikely to be accepted in the App Store, but you can still distribute it on your own.

How to make your SwiftUI MacOS App open a file in the same app window

By default, you MacOS app will open a new window each time you open a new file with your program. You rarely will need this, not exactly sure why one would make this behaviour the default, but here is how “fix it”

WindowGroup {
   ContentView().preferredColorScheme(.dark)
                .handlesExternalEvents(preferring: Set(arrayLiteral: "pause"), allowing: Set(arrayLiteral: "*"))
                .frame(minWidth: 300, minHeight: 300)
        }
        .windowStyle(.hiddenTitleBar)
        .commands {
            CommandGroup(replacing: .newItem, addition: { })
   }.handlesExternalEvents(matching: Set(arrayLiteral: "*"))

Download the FastImage for MacOS source code here.

By Cosmin Dolha

Cosmin Dolha, born in 1982 in Arad, Romania, is a dedicated programmer and digital artist with over 19 years of experience in the field. Married to his best friend, Cosmin is a proud father of two wonderful boys.

Throughout his career, Cosmin has designed and developed web apps, RIAS, real-time apps, and mobile applications for clients in the United States. He has also created around 25 educational games using AS3 and Haxe and has spent a year working with Unity for VR, ECS, and C# for Oculus GO.

Presently, Cosmin focuses on using Swift (Apple) to build software tools that incorporate GPT and Azure Cognitive Services. His interests extend beyond programming and include art, music, photography, 3D modeling (Zbrush, Blender), behavioral science, and neuropsychology, with a particular focus on the processing of visual information.

Cosmin is an avid podcast listener, with Lex Fridman, Andrew Huberman, and Eric Weinstein among his favorites. His reading list can be found on Goodreads, providing further insight into his interests: https://www.goodreads.com/review/list/78047933?shelf=%23ALL%23

His top 10 songs, available as a YouTube playlist, showcase his taste in music: https://www.youtube.com/playlist?list=PL5aMgX67sX9XltpvlYoih7BRAZwMrckSB

For inquiries or collaboration, Cosmin can be reached via email at contact@cosmindolha.com.