Debugging shortcuts for UIKeyCommand

Apr 2, 2017
1 minute read

I recently re-discovered UIKeyCommand while listening to Caleb Davenport’s, podcast, Runtime. He’s also got a blog post which shows you exactly how simple it is to create UIKeyCommand shortcuts for your app.

After reading that, I decided that it would be neat to implement them across my app, so I could also start navigating around my UI with lightning speed while I’m debugging in the simulator. I quickly realized that by using Swift extensions, I could automatically get these behaviors for free throughout our entire app.

Below is a code snippet which you can drop into your app to help you speed up your workflow. With just one tap on your keyboard, you’ll be able to pop a UIViewController from a navigation stack and dismiss any presented UIViewController.

extension UIViewController {

    open override var keyCommands: [UIKeyCommand]? {
        return [
            UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: [], action: #selector(popViewControllerWithKeyCommand)),
            UIKeyCommand(input: UIKeyInputDownArrow, modifierFlags: [], action: #selector(dismissViewControllerWithKeyCommand)),
        ]
    }

}

private extension UIViewController {

    dynamic func popViewControllerWithKeyCommand() {
        self.navigationController?.popViewController(animated: true)
    }

    dynamic func dismissViewControllerWithKeyCommand() {
        self.dismiss(animated: true, completion: nil)
    }

}

Don’t forget, you can make your own default shortcuts too.

Happy debugging!

Joe Fabisevich is an indie developer creating software at Red Panda Club Inc. while writing about design, development, and building a company. Formerly an iOS developer working on societal issues @Twitter. These days I don't tweet, but I do post on Threads.

Like my writing? You can keep up with it in your favorite RSS reader, or get posts emailed in newsletter form. I promise to never spam you or send you anything other than my posts, it's just a way for you to read my writing wherever's most comfortable for you.

If you'd like to know more, wanna talk, or need some advice, feel free to sign up for office hours, I'm very friendly. 🙂