User Agents

Web browsers and everyday web tech, explained

Keyboard Extensions in iOS: Development and Usage

· 6 min read · Extensions

In this article
  1. Technical Background and Architecture
  2. How Keyboard Extensions Work
  3. Full Access and Security Implications
  4. Common Development Challenges
  5. Comparing Custom Keyboards to Standard Input
  6. Frequently Asked Questions

A keyboard extension in iOS development is a specialized type of app extension that allows developers to create a custom system-wide keyboard that users can enable in their device settings. In technical terms, it is a separate binary that runs as a background process, providing a custom user interface (UI) for text input that replaces or augments the default Apple keyboard. This allows the keyboard to be used across any app that accepts text input, such as Messages, Mail, or third-party social media platforms, rather than being limited to a single application.

Technical Background and Architecture

Keyboard extensions are built using the UIInputViewController class, which is a subclass of UIViewController. Unlike a standard iOS application that has its own AppDelegate and main window, a keyboard extension is hosted by the system. When a user taps a text field and selects the custom keyboard, the iOS system instantiates the extension and presents its view.

The architecture is designed around a strict sandbox. Because a keyboard is a high-risk entry point for sensitive data, Apple imposes significant memory limits on these extensions. If a keyboard extension exceeds its allocated memory—which is often quite low compared to a full app—the system will terminate the process immediately, causing the keyboard to crash or revert to the default system keyboard. This requires developers to be extremely efficient with image assets and memory management.

The primary communication channel between the keyboard and the active text field is the textDocumentProxy. This object allows the extension to perform basic actions such as inserting text, deleting characters, and adjusting the cursor position. However, for security reasons, the textDocumentProxy does not provide the keyboard with the full content of the text field; it only provides a small window of text surrounding the current cursor position.

How Keyboard Extensions Work

The lifecycle of a keyboard extension begins when the user enables it in Settings > General > Keyboard. Once enabled, the system manages the switching mechanism (the globe icon). The operational flow typically follows these steps:

  1. Initialization: The system loads the extension binary and calls viewDidLoad on the UIInputViewController.
  2. Layout: The developer defines the keyboard layout using Auto Layout or SwiftUI. Since keyboards must adapt to different screen sizes and orientations, flexible constraints are mandatory.
  3. Input Handling: When a user taps a key, the extension calls methods on the textDocumentProxy, such as insertText("a").
  4. State Management: The keyboard tracks the current input mode (e.g., switching between alphabetic and numeric layouts) and updates the UI accordingly.
  5. Termination: When the user switches back to the system keyboard or closes the keyboard, the extension is suspended or terminated by the OS.

To handle complex interactions, developers often implement a "Next Keyboard" button. This is a requirement for App Store approval; users must always have a clear and immediate way to return to the system keyboard or another installed extension.

Full Access and Security Implications

One of the most critical distinctions in iOS keyboard development is the difference between standard access and "Full Access." By default, keyboard extensions are heavily restricted to prevent them from spying on user keystrokes or sending private data to a remote server.

  • Standard Access: The keyboard can insert and delete text but cannot access the network, use the clipboard (pasteboard), or access shared app groups.
  • Full Access: When a user manually toggles "Allow Full Access" in the iOS settings, the extension gains the ability to make network requests and read/write to a shared container (App Group) used by the main companion app.

From a safety perspective, Full Access is a significant privacy risk. A malicious keyboard with Full Access could theoretically act as a keylogger, capturing every password and credit card number typed by the user and transmitting it to a remote server. Because of this, Apple displays a prominent warning to the user before they enable Full Access. Developers must clearly justify why their keyboard needs this permission—for example, to provide cloud-based translation or to sync custom dictionaries across devices.

Common Development Challenges

Developing a keyboard extension is notably more difficult than building a standard app due to the constraints of the environment. One of the most common problems is the "Memory Pressure Crash." Because the keyboard shares resources with the host app, any leak or excessive use of high-resolution images will lead to a crash. Developers often have to use compressed assets or programmatically generate simple shapes instead of using large PNGs.

Another challenge is the lack of access to the full text context. Because the textDocumentProxy only provides a limited snippet of text, implementing features like advanced autocorrect or grammar checking is difficult. The keyboard does not know the entire contents of the document it is editing, which means it cannot analyze the overall context of a paragraph to suggest a correction.

UI responsiveness is also a hurdle. Since the keyboard must appear instantly when the user switches, any heavy initialization logic in viewDidLoad can cause a noticeable lag, leading to a poor user experience. Developers typically use lazy loading for non-essential components to ensure the initial render is snappy.

Comparing Custom Keyboards to Standard Input

When deciding whether to implement a keyboard extension, it is helpful to compare it against other input methods, such as a custom input view within a single app.

  • System-wide Reach: A keyboard extension works in every app on the phone. A custom inputView only works within the developer's own application.
  • User Friction: Extensions require the user to go into the system settings to enable them, which is a high barrier to entry. An inputView is automatic.
  • Capability: Custom inputViews have full access to the app's data and network without needing a separate "Full Access" toggle. Extensions are sandboxed.
  • Maintenance: Extensions must be tested against a vast array of third-party apps to ensure they don't cause crashes or layout glitches in different environments.

Frequently Asked Questions

Can a keyboard extension read passwords?

iOS prevents keyboard extensions from being active in "secure text entry" fields. When a user taps a password field, the system automatically switches back to the default system keyboard to ensure that sensitive credentials are not captured by third-party extensions.

Why does my custom keyboard keep crashing?

The most likely cause is exceeding the memory limit. Keyboard extensions have a very small memory footprint. Check for large image assets, memory leaks in your view controllers, or excessive caching. Using the Xcode Memory Graph Debugger can help identify these spikes.

Do I need a main app to have a keyboard extension?

Yes, a keyboard extension must be bundled within a containing app. While the user may never actually open the main app after enabling the keyboard, the app serves as the delivery mechanism and the place where the user can configure the keyboard's settings.

How do I let users switch back to the default keyboard?

You must implement a button that calls the advanceToNextInputMode() method on the UIInputViewController. This tells the system to cycle to the next available keyboard in the user's list, which is a mandatory requirement for App Store guidelines.

Can a keyboard extension use the internet?

Only if the user has explicitly granted "Full Access" in the device settings. Without this permission, all network requests are blocked by the system sandbox to protect user privacy.