<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Yousri on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Yousri on Medium]]></description>
        <link>https://medium.com/@ahmedyousrifci?source=rss-710fcb27c4b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*g8r_wNc45wEGPk-R</url>
            <title>Stories by Ahmed Yousri on Medium</title>
            <link>https://medium.com/@ahmedyousrifci?source=rss-710fcb27c4b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:33:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmedyousrifci/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[iOS Custom Keyboard]]></title>
            <link>https://medium.com/@ahmedyousrifci/ios-custom-keyboard-a4dbf1bbe165?source=rss-710fcb27c4b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4dbf1bbe165</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios-custom-keyboard]]></category>
            <category><![CDATA[custom-keyboard]]></category>
            <category><![CDATA[ios-apps]]></category>
            <dc:creator><![CDATA[Ahmed Yousri]]></dc:creator>
            <pubDate>Tue, 01 Sep 2026 17:18:11 GMT</pubDate>
            <atom:updated>2026-09-01T19:03:43.884Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*csawagyCRy5c8lisG6PQzA.png" /></figure><p>Agenda:</p><ul><li><strong>How to create a keyboard extension</strong>.</li><li><strong>How to make a secure channel between the app and the keyboard extension</strong>.</li><li><strong>How to debug the keyboard extension</strong></li></ul><h3><strong>How to create a keyboard extension</strong>.</h3><h3>Creating :</h3><p>1- Go to the <strong>targets</strong> list</p><p>2- Add a new target by clicking the “<strong>+</strong>” sign at the bottom of the list.</p><p>3- Search for “<strong>keyboard</strong>”; the new target will appear. Select it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nGX1UuaQRsBiEqN6I1wDjQ.png" /></figure><p>4- Click <strong>Next</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yc3bNniEfvwryw_P0ir0AA.png" /></figure><p>Now you’ve added the new target to your project. You’ll notice that a new Info.plist file, dedicated to the keyboard target only, has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*YvECQi6WLYXJTEFxXi1ksQ.png" /></figure><h3>Create views :</h3><p>The KeyboardViewController you get by default only manages a single root view — so if your keyboard has multiple states (e.g. a letters view and a numbers/symbols view), you&#39;ll build each as its own UIView subclass and swap them in and out, rather than cramming all your UI into the one view Apple gives you. And take care of the size of the views to make it fully dynamic.</p><p>1- First, we need to create a custom view to serve as the landing page for the keyboard. We’ll name it CustomInitialView.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*GyzHQyJt8YdXslTQ3YIAvA.png" /><figcaption>CustomInitialView XIB to be our initial view for the keyboard</figcaption></figure><p>2- Now we need to initialize it inside the KeyboardViewController.</p><pre>class KeyboardViewController: UIInputViewController {<br>    <br>    //MARK - subViews<br>    private var customInitialView: UIView!<br><br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        self.loadCustomView()<br>    }<br><br>    override func updateViewConstraints() {<br>        super.updateViewConstraints()<br><br>        if heightConstraint == nil {<br>            guard let inputView = inputView else { return }<br><br>            heightConstraint = inputView.heightAnchor.constraint(<br>                equalToConstant: 250<br>            )<br><br>            // Don&#39;t fight Apple&#39;s keyboard container constraint<br>            heightConstraint?.priority = .defaultHigh<br>            heightConstraint?.isActive = true<br>        }<br>    }<br>}<br><br>//MARK: - main configuration<br>extension KeyboardViewController{<br>    private func loadCustomView(){<br>        if customInitialView == nil {<br>            let customViewName = &quot;CustomInitialView&quot;<br>            let keyboardNib = UINib(nibName: customViewName, bundle: Bundle(for: KeyboardViewController.self))<br>            customInitialView = keyboardNib.instantiate(withOwner: self, options: nil)[0] as? UIView<br>        }<br>        <br>        self.showCustomView()<br>    }<br>    <br>    private func showCustomView(){<br>        // delete all subViews before add new subView<br>        for view in self.view.subviews {<br>            view.removeFromSuperview()<br>        }<br>        <br>        self.view.addSubview(customInitialView)<br>        customInitialView.translatesAutoresizingMaskIntoConstraints = false<br>        NSLayoutConstraint.activate([<br>            customInitialView.leftAnchor.constraint(equalTo: self.view.leftAnchor),<br>            customInitialView.rightAnchor.constraint(equalTo: self.view.rightAnchor),<br>            customInitialView.topAnchor.constraint(equalTo: self.view.topAnchor),<br>            customInitialView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)<br>        ])<br>        <br>        self.view.bringSubviewToFront(customInitialView)<br>    }<br>    <br>    private func add(subView: UIView){<br>        // delete all subViews before add new subView<br>        for view in self.customInitialView.subviews {<br>            view.removeFromSuperview()<br>        }<br>        <br>        self.customInitialView.addSubview(subView)<br>        subView.translatesAutoresizingMaskIntoConstraints = false<br>        NSLayoutConstraint.activate([<br>            subView.leftAnchor.constraint(equalTo: self.customInitialView.leftAnchor),<br>            subView.rightAnchor.constraint(equalTo: self.customInitialView.rightAnchor),<br>            subView.topAnchor.constraint(equalTo: self.customInitialView.topAnchor),<br>            subView.bottomAnchor.constraint(equalTo: self.customInitialView.bottomAnchor)<br>        ])<br>        <br>        self.customInitialView.bringSubviewToFront(subView)<br>    }<br>}</pre><p>We can now create our subviews; for example, we’ll create FirstViewand make it visible in KeyboardViewController so it can be easily displayed from any screen, triggered by any action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*JCrH4cJvvqEh830L4-l24w.png" /><figcaption>First View XIB with it’s viewModel</figcaption></figure><pre>import UIKit<br><br>class KeyboardViewController: UIInputViewController {<br>    <br>    //MARK - subViews<br>    private var customInitialView: UIView!<br>    private var firstView = FirstView()<br>    <br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        self.loadCustomView()<br>    }<br><br>    override func updateViewConstraints() {<br>        super.updateViewConstraints()<br><br>        if heightConstraint == nil {<br>            guard let inputView = inputView else { return }<br><br>            heightConstraint = inputView.heightAnchor.constraint(<br>                equalToConstant: 250<br>            )<br><br>            // Don&#39;t fight Apple&#39;s keyboard container constraint<br>            heightConstraint?.priority = .defaultHigh<br>            heightConstraint?.isActive = true<br>        }<br>    }<br>}<br><br>//MARK: - main configuration<br>extension KeyboardViewController{<br>    private func loadCustomView(){<br>        if customInitialView == nil {<br>            let customViewName = &quot;CustomInitialView&quot;<br>            let keyboardNib = UINib(nibName: customViewName, bundle: Bundle(for: KeyboardViewController.self))<br>            customInitialView = keyboardNib.instantiate(withOwner: self, options: nil)[0] as? UIView<br>        }<br>        <br>        self.showCustomView()<br>    }<br>    <br>    private func showCustomView(){<br>        // delete all subViews before add new subView<br>        for view in self.view.subviews {<br>            view.removeFromSuperview()<br>        }<br>        <br>        self.view.addSubview(customInitialView)<br>        customInitialView.translatesAutoresizingMaskIntoConstraints = false<br>        NSLayoutConstraint.activate([<br>            customInitialView.leftAnchor.constraint(equalTo: self.view.leftAnchor),<br>            customInitialView.rightAnchor.constraint(equalTo: self.view.rightAnchor),<br>            customInitialView.topAnchor.constraint(equalTo: self.view.topAnchor),<br>            customInitialView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)<br>        ])<br>        <br>        self.view.bringSubviewToFront(customInitialView)<br>    }<br>    <br>    private func add(subView: UIView){<br>        // delete all subViews before add new subView<br>        for view in self.customInitialView.subviews {<br>            view.removeFromSuperview()<br>        }<br>        <br>        self.customInitialView.addSubview(subView)<br>        subView.translatesAutoresizingMaskIntoConstraints = false<br>        NSLayoutConstraint.activate([<br>            subView.leftAnchor.constraint(equalTo: self.customInitialView.leftAnchor),<br>            subView.rightAnchor.constraint(equalTo: self.customInitialView.rightAnchor),<br>            subView.topAnchor.constraint(equalTo: self.customInitialView.topAnchor),<br>            subView.bottomAnchor.constraint(equalTo: self.customInitialView.bottomAnchor)<br>        ])<br>        <br>        self.customInitialView.bringSubviewToFront(subView)<br>    }<br>}<br><br>//MARK: - firstView configuration<br>extension KeyboardViewController{<br>    private func showFirstView(){<br>        firstView = FirstView(viewModel: FirstViewViewModel())<br>        add(subView: firstView)<br>        configureFirstViewActions()<br>    }<br>    <br>    private func configureFirstViewActions(){<br>        openNextScreenFormFirstViewCallBack()<br>    }<br>    <br>    private func openNextScreenFormFirstViewCallBack(){<br>        firstView.nextScreenHandler { [weak self] in<br>            guard let self else {return}<br>            // go to another subView<br>            add(subView: &lt;#T##UIView#&gt;)<br>        }<br>    }<br>}</pre><p>Every view needs to be loaded inside itself, so we need to add a Util file that contains the function that loads every view inside its initView() function</p><pre>extension UIView {<br>    /// Loads a view from a nib file and adds it as a subview to the current view instance..<br>    func loadViewFromNib(bundle: Bundle? = nil) {<br>        let nibName = String(describing: Self.self)<br>        let bundle = Bundle(for: Self.self)<br>        let nib = UINib(nibName: nibName, bundle: bundle)<br><br>        guard let contentView = nib.instantiate(withOwner: self).first as? UIView else {<br>            assertionFailure(&quot;unable to find the content view&quot;)<br>            return<br>        }<br><br>        contentView.frame = bounds<br>        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]<br>        addSubview(contentView)<br>    }<br>}</pre><p>Now, here’s a diagram that describes the flow of the keyboard we created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fh9prS4A8sxMSQrQQ4XMWQ.png" /><figcaption>keyboard flow diagram</figcaption></figure><h3><strong>How to make a secure channel between the app and the keyboard extension.</strong></h3><p>As we described before, we create a target that’s still related to our main project. Depending on your business logic, the keyboard may need some data from your app, for example, sending the user’s accessToken or other data to use when making API calls or handling dependencies inside the keyboard.</p><p>We now have two main points:</p><ul><li>channel (AppGroup)</li><li>keychain</li></ul><p>Let us take it one by one</p><p>1- <strong>channel (App Group)</strong>: <br>We need to create a Keychain group to establish a channel between the targets. Each target must reference this same Keychain group in its capabilities. The most effective way to name it is like this:</p><pre>{AppleTeamId}.{BundleIdentifier}</pre><ul><li>AppleTeamId: you can get this from the Apple Developer profile</li><li>BundleIdentifier: you already have it inside your app</li></ul><p>You can check <a href="https://developer.apple.com/documentation/xcode/configuring-keychain-sharing"><strong>Apple Docs</strong></a>.</p><p>So now we need to add a new keychain group <br>1- Open your project&#39;s main target</p><p>2- Open the Capabilities tab</p><p>3- Click plus to add a new Capability</p><p>4- search for “<strong>keychain</strong>”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hAC0UZ6uLQ2cTxiRjaCYUA.png" /><figcaption>add new keychain sharing capability</figcaption></figure><p>5- We’ll add our first Keychain group name. When you add a new one, it will automatically append your bundle identifier, so you just need to add your team ID, then the {BundleIdentifier}.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K72zay_hGIjvL3OCebpeRA.png" /></figure><p>6- To share data between the main target and our keyboard target, we need to add this same Keychain group with the exact same name inside the keyboard target as well. {<strong>required</strong>}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fM6tjgyJiv1syKdsBaLvCQ.png" /></figure><p>Now that we have the channel, we need to create a manager that saves data to the Keychain.</p><p>1- Create KeychainManager</p><pre>import Foundation<br>import Security<br><br>class KeychainManager {<br>    static let shared = KeychainManager()<br>    <br>    private let accessGroup = &quot;{teamID}.com.personal.customKeyboard&quot;<br><br>    private init() {}<br><br>    // Save value<br>    func save(key: String, value: String?) {<br>        if let value = value,<br>           let data = value.data(using: .utf8) {<br>            let query: [String: Any] = [<br>                kSecClass as String: kSecClassGenericPassword,<br>                kSecAttrAccount as String: key,<br>                kSecValueData as String: data,<br>                kSecAttrAccessGroup as String: accessGroup<br>            ]<br>            // Delete any existing value<br>            SecItemDelete(query as CFDictionary)<br>            // Add new one<br>            SecItemAdd(query as CFDictionary, nil)<br>        }<br>    }<br><br>    // Retrieve value<br>    func get(for key: String) -&gt; String? {<br>        let query: [String: Any] = [<br>            kSecClass as String: kSecClassGenericPassword,<br>            kSecAttrAccount as String: key,<br>            kSecReturnData as String: true,<br>            kSecMatchLimit as String: kSecMatchLimitOne,<br>            kSecAttrAccessGroup as String: accessGroup<br>        ]<br>        <br>        var dataTypeRef: AnyObject?<br>        let status = SecItemCopyMatching(query as CFDictionary, &amp;dataTypeRef)<br>        <br>        guard status == errSecSuccess,<br>              let data = dataTypeRef as? Data,<br>              let value = String(data: data, encoding: .utf8)<br>        else {<br>            return &quot;Keychain read failed with status: \(status)&quot;<br>        }<br>        <br>        return value<br>    }<br>    <br>    private func deleteKey(_ key: String) {<br>        let query: [String: Any] = [<br>            kSecClass as String: kSecClassGenericPassword,<br>            kSecAttrAccount as String: key<br>        ]<br>        SecItemDelete(query as CFDictionary)<br>    }<br>}</pre><p>2- This file must be assigned to the targets that use our Keychain group. To do this, open the <strong>File Inspector</strong> on the right, click the “<strong>+</strong>” under Target Membership, and add the keyboard target.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vj8Yi8YeL9ZDZPz8LDNAVw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z14HdhO6aAEoaRi_XhSdIQ.png" /></figure><p>Now you can read and write any value from any target. This requires two things:</p><p>1- Add the target to the<strong> </strong><strong>KeychainManager </strong>file so it can access the code.</p><p>2- Add the Keychain group name to the target’s Capabilities for the target you want to grant access to the shared data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SF_Ig_4QG1mEJZg5jOSt6g.png" /><figcaption>be aware of this</figcaption></figure><p><strong>How to manage this warning?</strong></p><p>You can create a separate view that shows this warning every time the keyboard opens, to remind the user that you need to enable full access.<br>1- Create the UIView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*HTa9hoJ64Q4XJGZOC4XWDA.png" /><figcaption>custom view for enable access warning</figcaption></figure><p>2- Add the code to manage it</p><pre>class KeyboardViewController: UIInputViewController {<br><br>    //MARK - subViews<br>    private var proxy : UITextDocumentProxy!<br>    private var customInitialView: UIView!<br>    private var heightConstraint: NSLayoutConstraint?<br>    private var firstView = FirstView()<br>    private var secondView = SecondView()<br>    private var noFullAccessEnabledView = NoFullAccessEnabledView()<br>    <br>    //MARK: - didload<br>    override func viewDidLoad() {<br>        super.viewDidLoad()<br>        self.loadCustomView() //show the initial view<br>        self.configureFirstView() //show the view that depends on the full access check<br>        self.proxy = textDocumentProxy as UITextDocumentProxy<br>    }<br>    <br>    private func isFullAccessEnabled() -&gt; Bool {<br>        return hasFullAccess<br>    }<br>    <br>    private func configureFirstView() {<br>        if !isFullAccessEnabled(){<br>            showNoFullAccessView()<br>            return<br>        }<br>        <br>        showFirstView()<br>    }<br>    <br>}</pre><h3><strong>How to debug the keyboard extension</strong></h3><p>This is important because keyboard extensions fail most of the time silently. If the extension crashes, for example, from hitting its memory limit, the user sees the keyboard disappear, with no error shown on screen. Without a proper way to catch this, you’d have no idea what went wrong.</p><p>You can do that by using one of these methods:</p><p><strong>1. Use the Scheme:</strong></p><ul><li>In Xcode, switch the active scheme to your keyboard extension target.</li><li>Run it (Cmd+R).</li><li>Xcode will prompt you to choose a host app; select any app with a text field (e.g. Notes or Messages).</li><li>Wait for the host app to launch, then switch to your custom keyboard and tap into a text field.</li><li>If it attaches successfully, your breakpoints will go live, but this method isn’t always reliable</li></ul><p><strong>2. Use “Attach to Process”:</strong></p><ul><li>Run your keyboard extension on the device first.</li><li>Open the keyboard (switch to it via the globe key in any text field).</li><li>In Xcode, go to <strong>Debug</strong>.</li><li>Select <strong>Attach to Process</strong>.</li><li>Wait a second; your keyboard’s bundle identifier will appear in the list.</li><li>Select your keyboard’s bundle identifier.</li><li>Any breakpoint you’ve added inside a file in the keyboard target will now be hit.</li></ul><p><strong>3. Use Console.app to detect crashes:</strong></p><ul><li>Open <strong>Console.app</strong> on your Mac (Spotlight search → “Console”).</li><li>Connect your iPhone and select it from the sidebar.</li><li>In the search bar, filter by your keyboard extension’s process name.</li><li>Reproduce the crash by switching to your keyboard and typing.</li><li>If it crashes, you’ll see the crash log appear in Console; this works even without Xcode attached.</li></ul><p>We also declare a proxy property, which represents the text field the user is currently typing in. We&#39;ll use it to insert or delete text on the user&#39;s behalf.<br>That’s the plain-language versionUITextDocumentProxy is the object that lets your keyboard actually type into whatever app the user has open, so it&#39;s worth a one-line explanation of why it exists, not just that it&#39;s declared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CaC66DqeitdvA1QQS36Hxw.png" /><figcaption>how to set text inside text input</figcaption></figure><h3><strong>Restrictions:</strong></h3><ul><li>Cannot open the main app directly; keyboard extensions run in a separate process with no access to the host app’s APIs.</li><li>Cannot open system Settings; extensions don’t have permission to launch other apps for security reasons.</li><li>Cannot access the camera or microphone even with Full Access granted.</li><li>Cannot make network requests unless Full Access is granted by the user.</li><li>Cannot access location services, photo library, or contacts without Full Access.</li><li>Cannot access the main app’s Documents or Library folders; you must use App Groups for shared storage.</li><li>Memory is severely limited; keyboard extensions run on a tight memory budget and can be silently killed if they exceed it.</li><li>Cannot perform background tasks or send notifications; the extension only runs while the user is actively typing.</li></ul><p>I have attached a sample project with a simple implementation to guide you and get all that you need <br>. You can see it here: <a href="https://github.com/AhmedU3Swift/IOS-Custom-Keyboard.git"><strong><em>sampleApp</em></strong></a></p><p><strong>Wrapping Up</strong></p><p>Building a custom keyboard on iOS comes with its own set of challenges, from setting up the extension target to securely sharing data with your main app through App Groups and Keychain, to debugging a process that doesn’t behave like a normal app.</p><p>Once these pieces are in place, though, the workflow becomes much easier to repeat: creating new views, adding features, and extending your keyboard’s functionality is straightforward once the foundation the target setup, the shared channel, and a reliable debugging process is solid.</p><p>I hope this article saved you some of the trial and error I went through while building mine. If you have questions or run into issues not covered here, feel free to leave a comment; I’ll do my best to help.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4dbf1bbe165" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>