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.
Without the implementation, the user was able to choose any custom keyboards as demonstrated here: