Combining SDL3 with SwiftUI
The article discusses the integration of SDL3 with SwiftUI, focusing on challenges and solutions for managing application lifecycle conflicts. It highlights issues such as keyboard input interference and game controller input handling when both frameworks are used together. The article provides specific code implementations to address these problems effectively.
- ▪SDL3 and SwiftUI both assume control over the application lifecycle, leading to conflicts.
- ▪A custom UITextField subclass, KeyLockedTextField, is introduced to manage keyboard input issues.
- ▪Game controller input handling is adjusted by toggling user interaction settings during dialog presentation.
Opening excerpt (first ~120 words) tap to expand
SDL3 + SwiftUI Coexistence Demo Minimal example showing how to present a SwiftUI dialog from a C + SDL3 application on iOS, tvOS, and macOS, with working text fields and game controller input. The Problem SDL3 and SwiftUI both assume they own the application lifecycle. When they coexist, three specific collisions occur: 1. SDL steals the keyboard (iOS / tvOS) SDL3 observes UITextFieldTextDidChangeNotification. Every time the user types a character, SDL calls SDL_StartTextInputWithProperties, which calls becomeFirstResponder on SDL's hidden UITextField. This forces resignFirstResponder on whichever field the user is actually typing in, dismissing the keyboard mid-sentence. Fix: KeyLockedTextField — a UITextField subclass that overrides canResignFirstResponder to return false while editing.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at GitHub.