- Published on
- 2 min read
> Preventing the software keyboard from dismissing on iOS simulators
Over the years I've seen UI bugs make their way into forms on many occasions because developers often test using the iOS simulators. When testing using the simulator, it is second nature to start typing on your physical keyboard to enter text into a field. Doing so automatically dismisses the software keyboard. When this happens, it can be easy to ship code where some UI elements are hidden under the keyboard when it pops up.
The solution I've found is to place this snippet somewhere that will be run once early in your app's lifecycle. I usually use applicationDidFinishLaunching. Doing so will allow you to type with the physical keyboard without dismissing the software keyboard, thus making it easier to make sure your UI is behaving properly.
#if targetEnvironment(simulator)
let selector = NSSelectorFromString("setHardwareLayout:")
typealias SetHardwareLayoutImp = @convention(c) (
UITextInputMode,
Selector
) -> Void
for inputMode in UITextInputMode.activeInputModes {
if inputMode.responds(to: selector) {
guard let imp = inputMode.method(for: selector) else {
continue
}
let function = unsafeBitCast(imp, to: SetHardwareLayoutImp.self)
function(inputMode, selector)
}
}
#endif
Entering some text with both the software and hardware keyboards.

// Continue_Learning
Getting Started with App Intents and Siri Shortcuts
Expose your app's features to Siri, Shortcuts, and Spotlight with App Intents, the Swift-native replacement for SiriKit in iOS 16 and later.
App Tracking Transparency in SwiftUI
A practical guide to App Tracking Transparency in iOS, covering the Info.plist usage description, the ATTrackingManager API, authorization statuses, and when to actually prompt the user from SwiftUI.
withCheckedContinuation vs withUnsafeContinuation in Swift
Continuations bridge completion-handler APIs into async/await. The checked variant catches the two ways you can get it wrong, and the unsafe one trusts you completely.
// Stay Updated
Get notified when I publish new tutorials on Swift, SwiftUI, and iOS development. No spam, unsubscribe anytime.