Is it possible to developer a SwiftUI app using only SwiftPM?

Hi everyone,

Just wondering: is it possible to developer a SwiftUI App using only the Swift Package Manager? I.e. no .xcodeproj?

I created an executable package, targeting macOS 14 only with a very simple scene/view:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

This builds and runs, but does not show a window. So I'm wondering: is this even possible?

Kind regards,

Maarten

Yes, at least on macOS you can add this initializer to your App type:

@main
struct MyApp: App {
  init() {
    DispatchQueue.main.async {
      NSApp.setActivationPolicy(.regular)
      NSApp.activate(ignoringOtherApps: true)
      NSApp.windows.first?.makeKeyAndOrderFront(nil)
    }
  }

  var body: some Scene {
    // ...
  }
}
12 Likes

That's amazing! Thanks @Max_Desiatov

1 Like

Depending on the APIs and entitlements you need, you could also create an "App Playground" project in Xcode and build and run it against a Catalyst destination. If you show the .swiftpm package contents, you will see it is mostly a Swift package under the hood.

Depends how strictly you mean "only the SPM": only Xcode's fork of SPM brings in the AppleProductTypes module, I believe.

1 Like

My main objective is to use the simple modularization offered by SPM. And I’m only targeting macOS for now.

Note: I’m still using Xcode both as well as Viscose as the IDEs.

I haven’t encountered any issues so far, but also didn’t use any specific entitlements yet. Interesting to see where this goes.