Building a Native macOS Menu Bar Utility with SwiftUI
Explore a MenuBarExtra scene, an observable data model, and an optional login-item manager. The on-page interface is a browser simulation; use Xcode to build native Swift code.
Interactive Sandbox & Menu Bar Engine
import SwiftUI
import AppKit
@main
struct MenuBarMonitorApp: App {
@StateObject private var telemetry = SystemTelemetry()
var body: some Scene {
MenuBarExtra("Silicon Pulse", systemImage: "cpu.fill") {
VStack(alignment: .leading, spacing: 12) {
Text("Silicon Pulse").font(.headline)
Text(telemetry.summary).font(.caption)
Button("Update sample") { telemetry.updateSample() }
Divider()
Button("Quit") { NSApplication.shared.terminate(nil) }
}
.padding(16)
.frame(width: 260)
}
.menuBarExtraStyle(.window)
}
}
SiliconPulse
Apple M4 Max • 16-Core
Complete Step-by-Step Implementation
Follow the source example to understand a menu bar scene, observable state, and an optional login-item manager. Production telemetry, signing, and distribution require additional implementation and testing.
System Prerequisites
Ensure your development workstation meets these toolchain baselines before compiling.
The App Lifecycle Without NSApplicationDelegate
Traditional macOS menu bar apps required an Objective-C or AppKit boilerplate relying on NSStatusItem and subclassing NSApplicationDelegate.
With modern SwiftUI in macOS 13+, the entire application can be expressed as a pure declarative MenuBarExtra scene. To completely hide the icon from the macOS Dock, add a single key to your Info.plist:
<true/>
import SwiftUI
import AppKit
@main
struct MenuBarMonitorApp: App {
@StateObject private var telemetry = SystemTelemetry()
var body: some Scene {
MenuBarExtra("Silicon Pulse", systemImage: "cpu.fill") {
VStack(alignment: .leading, spacing: 12) {
Text("Silicon Pulse").font(.headline)
Text(telemetry.summary).font(.caption)
Button("Update sample") { telemetry.updateSample() }
Divider()
Button("Quit") { NSApplication.shared.terminate(nil) }
}
.padding(16)
.frame(width: 260)
}
.menuBarExtraStyle(.window)
}
}
Model Sample Data with ObservableObject
Start with a main-actor model and published sample state. This keeps UI updates easy to understand before you introduce real system measurements. The downloaded example does not call kernel APIs.
Concurrency Note: In Swift 6, encapsulate Darwin calls within an isolated actor to satisfy data-race safety across asynchronous timer iterations.
import Foundation
import Combine
// Sample values for learning UI state. This does not read hardware sensors.
@MainActor
final class SystemTelemetry: ObservableObject {
@Published var summary = "Demo values — not live telemetry"
private var updates = 0
func updateSample() {
updates += 1
summary = "Sample update \(updates) — simulated data"
}
}
Launch at Login via SMAppService
Forget deprecated SMLoginItemSetEnabled or nested Helper App targets. Apple’s modern ServiceManagement framework lets your main application binary register itself directly with launchd.
The user maintains full transparency in System Settings > General > Login Items, and status inspection is instantaneous.
import ServiceManagement
// Optional: call from a signed app after explaining the user's choice.
// This helper is not invoked by the sample UI.
@MainActor
final class LaunchAgentManager {
func setEnabled(_ enabled: Bool) throws {
if enabled {
try SMAppService.mainApp.register()
} else {
try SMAppService.mainApp.unregister()
}
}
}
Sandboxing, Notarization & Distribution
To distribute outside the Mac App Store while avoiding Gatekeeper warnings, sign your binary with Developer ID Application credentials and staple notarization tickets. For reading non-standard hardware sensors (e.g. discrete GPU temperatures), understand when to request com.apple.security.temporary-exception.iokit-user-client-class.
Watch Apple’s Swift Playgrounds introduction
A related introduction from Apple Developer. This video teaches SwiftUI app creation; it is not an original Tech2Apple recording of this menu bar project.
Next Tutorials in macOS Track
Writing Metal Compute Kernels for SwiftUI Shader Effects
Hook native MSL shaders directly into the SwiftUI `.colorEffect` modifier for zero-latency GPU particle fields.
Local LLM Integration with Apple MLX Swift API
Run 4-bit quantized Llama 3 models on Mac unified memory with streaming tokens via native Swift async pipelines.
Building iOS Lock Screen Live Activities
Push real-time ActivityKit status changes and Dynamic Island states across wireless Apple ecosystem links.
Keep exploring Apple development
Follow the published guide collection through RSS. Original videos and measured benchmark reports will be added when available.