A Swift package providing native desktop application APIs with cross-platform window management, application lifecycle control, and system integration features.
- AppRunner: Application lifecycle management and event loop integration
- Window Management: Create, configure, and control native windows
- Display Management: Multi-display support and screen information
- System Integration: Tray icons, keyboard monitoring, and accessibility features
- Cross-Platform: Support for macOS, with planned support for Windows and Linux
Create a basic native application with just a few lines of code:
import NativeAPI
// Run with default window
let exitCode = runApp()
print("App exited with code: \(exitCode)")
import NativeAPI
// Configure window options
let options = WindowOptions()
_ = options.setTitle("My Swift App")
options.setSize(Size(width: 1000, height: 700))
options.setMinimumSize(Size(width: 500, height: 350))
options.setCentered(true)
// Run with custom options
let exitCode = runApp(with: options)
print("App exited with code: \(exitCode)")
import Foundation
import NativeAPI
@MainActor
class MyApplication {
private var mainWindow: Window?
func initialize() -> Bool {
return WindowManager.shared.initialize()
}
func setupWindow() -> Bool {
let options = WindowOptions()
_ = options.setTitle("Advanced App")
options.setSize(Size(width: 1200, height: 800))
guard let window = WindowManager.shared.createWindow(with: options) else {
return false
}
self.mainWindow = window
return true
}
func run() -> AppExitCode {
guard let window = mainWindow else {
return .invalidWindow
}
window.show()
return AppRunner.shared.run(with: window)
}
}
let app = MyApplication()
guard app.initialize() && app.setupWindow() else {
exit(1)
}
let exitCode = app.run()
exit(exitCode.rawValue)
Add this package to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/leanflutter/nativeapi-swift", from: "1.0.0")
]
Or add it through Xcode:
- File β Add Package Dependencies
- Enter the repository URL
- Select the version and add to your target
Manages application lifecycle and runs the native event loop:
// Singleton access
let appRunner = AppRunner.shared
// Check if running
if appRunner.isRunning {
print("Application is currently running")
}
// Run with window
let exitCode = appRunner.run(with: window)
Exit Codes:
.success
(0) - Normal exit.failure
(1) - Application error.invalidWindow
(2) - Invalid window provided
Create and control native windows:
// Create window with options
let options = WindowOptions()
_ = options.setTitle("My Window")
options.setSize(Size(width: 800, height: 600))
let window = WindowManager.shared.createWindow(with: options)
// Window operations
window?.show()
window?.hide()
window?.minimize()
window?.maximize()
window?.focus()
// Window properties
window?.title = "New Title"
window?.size = Size(width: 1000, height: 800)
window?.position = Point(x: 100, y: 100)
window?.opacity = 0.9
window?.isAlwaysOnTop = true
Monitor window events:
let callbackId = WindowManager.shared.registerEventCallback { event in
switch event.type {
case .created:
print("Window created")
case .closed:
print("Window closed")
case .focused:
print("Window focused")
case .moved(let position):
print("Window moved to \(position)")
case .resized(let size):
print("Window resized to \(size)")
}
}
// Don't forget to unregister
_ = WindowManager.shared.unregisterEventCallback(callbackId)
Work with multiple displays:
let displayManager = DisplayManager.shared
let displays = displayManager.getAllDisplays()
for display in displays.displays {
print("Display: \(display.name)")
print("Resolution: \(display.size.width)x\(display.size.height)")
print("Scale: \(display.scaleFactor)")
}
// Get primary display
if let primary = displayManager.getPrimaryDisplay() {
print("Primary display: \(primary.name)")
}
Full support with native Cocoa integration:
#if os(macOS)
// Access native NSWindow
if let nsWindow = window.nsWindow {
// Perform macOS-specific operations
}
#endif
Planned support for future releases.
The package includes comprehensive examples:
- Basic Example (
Sources/Example/main.swift
): Simple usage patterns - Advanced Example (
Sources/Example/AppRunnerExample.swift
): Full application with event handling
Run examples:
swift run Example
Run the test suite:
# Run all tests
swift test
# Run specific tests
swift test --filter AppRunnerTests
swift test --filter WindowManagerTests
Detailed documentation is available:
- AppRunner Bindings - Application lifecycle management
- Window Bindings - Window management APIs
- macOS: 10.15 or later
- Swift: 6.0 or later
- Xcode: 16.0 or later (for development)
The package consists of:
- C++ Core Library (
libnativeapi
): Cross-platform native implementations - C API Layer (
capi
): C interface for Swift interoperability - Swift Bindings: High-level Swift APIs wrapping the C interface
Swift Application
β
Swift Bindings (NativeAPI)
β
C API Layer (CNativeAPI)
β
C++ Core Library (libnativeapi)
β
Platform APIs (Cocoa, Win32, X11)
Always initialize and cleanup properly:
// Initialize
guard WindowManager.shared.initialize() else {
exit(1)
}
// Use resources...
// Cleanup
defer {
WindowManager.shared.shutdown()
}
UI operations should be performed on the main thread:
@MainActor
class UIManager {
func updateWindow() {
// Safe to call UI methods here
window.show()
}
}
// Or use DispatchQueue when needed
DispatchQueue.main.async {
window.title = "Updated Title"
}
Handle failures gracefully:
guard let window = WindowManager.shared.createWindow(with: options) else {
print("Failed to create window")
return .failure
}
let exitCode = AppRunner.shared.run(with: window)
if exitCode != .success {
print("Application exited with error: \(exitCode)")
}
We welcome contributions! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Update documentation
- Submit a pull request
# Clone the repository
git clone https://github.com/leanflutter/nativeapi-swift
cd nativeapi-swift
# Build the project
swift build
# Run tests
swift test
# Run examples
swift run Example
This project is licensed under the MIT License - see the LICENSE file for details.
- nativeapi - Core C++ library
- nativeapi-dart - Dart bindings
- Issues - Bug reports and feature requests
- Discussions - Questions and community support
- Documentation - Detailed guides and tutorials