Skip to main content

Command Palette

Search for a command to run...

How to disable custom keyboards in iOS SwiftUI-based applications

Updated
2 min read
How to disable custom keyboards in iOS SwiftUI-based applications
M

I am a Software Engineer working on open source and enterprise mobile SDKs for iOS and MacOS developers written in Swift. From 🇩🇪 and happily living in 🇺🇸

In this blog post, I explain how you can prevent users from using custom keyboards in your iOS applications developed with SwiftUI.

Preventing custom keyboards is a common security requirement for mobile enterprise applications. More on Android than iOS, but it is good to know how to disable it if a product owner asks.

Custom keyboards are app extensions. If you want to learn how to create a custom keyboard and how to test it, then I recommend reading Create custom keyboards in iOS by David Martinez.

UIKit provides the application(_:shouldAllowExtensionPointIdentifier:) function on the UIApplicationDelegate protocol to grant or deny permission to use specific app extensions, particularly keyboard.

Implement the function in a custom class, then use your class with the UIApplicationDelegateAdaptor property wrapper in your SwiftUI app.

@main
struct MySwiftUIApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplication.ExtensionPointIdentifier) -> Bool {
        switch extensionPointIdentifier {
        case UIApplication.ExtensionPointIdentifier.keyboard:
            return false
        default:
            return true
        }
    }
}

The function gets called every time your user sets or removes the focus to a TextField .

By implementing application(_:shouldAllowExtensionPointIdentifier:) the user can be prevented from using custom keyboards, as those are not listed anymore within the iOS app.

No more custom keyboard selection

Without the implementation, the user was able to choose any custom keyboards as demonstrated here:

Custom Keyboard selection

More from this blog

Dev blog post potpourri by senior software engineer Marco Eidinger

149 posts

Hello 👋🏻 , I am a Software Engineer working on open source and enterprise mobile SDKs for iOS and MacOS developers written in Swift. From 🇩🇪 and happily living in 🇺🇸