Tag Archives: iOS 9

How to add your own shortcuts above the keyboard in iOS 9


Ever wondered how to create those new shortcut items that can appear above the iPad keyboard in iOS 9?

By default iOS will produce auto correction suggestions here since iOS 8, but since iOS 9 we can hook into this mechanism and provide our own bar button items on the left and right – much like we can with a navigation bar.

Let me show you how to do it.

UIBarButtonItems and the UITextInputAssistantItem

In the above screenshot we can see an icon at the left, and a single shortcut on the right. Those are instances of our old friend the UIBarButtonItem. In short, each item is added to an array, and each button in the array is displayed – if iOS has the space above the keyboard.

Apple doesn’t seem to have a name for this space, but the class that’s responsible for showing those shortcuts is called the UITextInputAssistantItem. There’s room for a leading and a trailing group of items.

This group isn’t just an array of buttons though; it’s another class called the UIBarButtonItemGroup, and the clever bit is that this group has a representative “action button” that is shown and will open the group if there’s not enough space. I know this sounds complicated, but bear with me here.

In the screenshot above, on the left hand side, we can see an “action button” (literally). iOS shows this because there’s not enough room to display the words “First Shortcut” and “Second Shortcut”. But when we press it, we’ll see something like this:


On the right hand side, the shortcut titles are short enough to be displayed and this representative button isn’t shown – however we still need to define one in case that space is getting tight.

Let’s see how to do this in code.

Continue reading

How to track finger position and touch force in iOS 9

Female holds and touch tablet computer isolated on white background

In iOS 9.1 the UITouch Class has been amended to track several new options:

  • the force property will track how hard the user presses onto the screen
  • the preciseLocationInView property will track positions more accurately
  • altitude and azimuth detection for Apple Pencil

Here’s how to track all these options in our view.

Continue reading

3D Touch in iOS 9, Part 2: Preview Actions

Photo Sep 26, 10 57 04

If you’ve implemented a Peek (or preview) in your app as described in my previous article, you may be interested in implementing Preview Actions as well. Those come up when the user triggers the Peek preview and swipes up. Notice the arrow icon at the top of the preview if such actions are available.

It’s easy to add them to your preview controller, although it’s not as clearly described in the Apple documentation as it perhaps should be. Here’s how to do it.

In a nutshell, we need to create an array of UIPreviewActions, each of which has a title, a style and a completion block. Then we replace this array in our preview view controller (it works much like overriding a method with our own). The result looks just like an UIActionSheet.

Those methods look a little scary because the parameters are so long, but code completion will gladly help you out.

Each Preview Action can have a style of default, destructive (red) or selected (with a tick mark). There’s no need to implement a delegate for reacting to the outcome of those actions, because each action has a completion block attached to it, in which you can define what shall happen if the user selects the action.

Preview Actions can in fact be grouped together to create submenus. The principle is very similar to the above:

  • create your actions and add them to an array
  • add the array to a UIPreviewActionGroup
  • repeat for other groups as necessary
  • add all groups to yet another array
  • return the array of action groups in previewActionItems

Here’s an example (with only a single group, based on the above):

There’s no need to deal with dismissals or transitions, iOS will handle this beautifully for us.

3D Touch in iOS 9, Part 1: Peek and Pop

Screen Shot 2015-09-26 at 09.48.21

With the introduction of the iPhone 6s we have some new ways of getting user input via 3D Touch. Now THAT’S innovative!

There are several things we can do with 3D Touch, and the first one we’ll explore here is Peek and Pop. When the user presses down a little harder, a Peek (or preview) appears, and if the user presses even harder, a Pop (or commit) view controller appears. Both are plain view controllers that are called via a delegate method we need to implement. Let’s see how this works.

Before we begin

The first thing we’ll need to do is to indicate that our view controller will conform to a new protocol. It’s called the UIViewControllerPreviewDelegate Protocol.

For backward compatibility I’ll also add a long press gesture recogniser here. Should our sample app be run on a device without 3D Touch support, at least the preview can be brought up via a long press gesture.

We’ll check if 3D Touch is available using something like the following method. To complete the setup, I’ve also included a custom initialiser for the long press gesture.

The above checks a new property of the UITraitCollection class called forceTouchCapability. If it returns the enum “available”, then the device is 3D Touch capable and it’s switched on. Note that at the time of writing, only the iPhone 6s and 6s Plus support this feature, and of course the iPad Pro when it arrives in November. Neither the iPhone 6 or earlier, nor the Simulator support the 3D Touch feature.

Because there’s a chance that the user may disable 3D Touch at any point, we can call this method in viewWillAppear. This will make sure it gets called when the app launches and when our main view comes back into vision. In that case, we’ll simple enable the long press gesture and the app keeps working. If the user switched 3D Touch on again, we’ll disable that gesture so it won’t interfere with 3D Touch.

We also want to call this method in traitCollectionDidChange, which is called when the user switches the 3D Touch feature on or off (among other occasions). Here’s how we do that:


With this prep work out of the way, let’s implement the view controllers.

Implementing Peek

iOS will call the previewingContext:viewControllerForLocation method when the user presses shallow. This is one of the two methods we need to conform to since we’ve volunteered for it with the above protocol.

All we have to do here is to return a dedicated view controller. I’m instantiating one from my storyboard here. The transition and background blur is automatically handled by iOS (thanks, team!)

I’ve noticed that iOS tries to do this multiple times and may throw an error message if our preview view is already being presented. To suppress this message I’m checking if a preview view is already being presented before presenting another one. Anything to make iOS happy!

You can get quite fancy with this method as you are passed the location in the view at which the user presses the screen. That’s how the Mail app figures out which cell to focus on.


Make sure to implement a method that can dismiss the preview view controller, otherwise it’ll just sit on top of the stack and not go away again.

When the user swiped up while the preview view is displayed, we can display what’s known as Preview Action items. I’ll discuss this in my next article.

Implementing Pop

Once the preview is presented, the user can elect to press deeper to switch to another view controller. Again, all we have to do is to conform to the second method of our above protocol. This one is called previewingContext:commitViewController:viewControllerToCommit.

Just like before, I’ll instantiate a view controller from my storyboard here.


And again, make sure your commit view controller has some navigational mechanism to be dismissed. You can dismiss either the preview or the commit view controller with something like this:

And that’s it in a nutshell!

There’s a lot we can do with 3D Touch, and I’ll cover more in the few next articles. Check out my demo project below for all those funky implementations, and don’t forget that 3D Touch can currently only be tested on real devices.

Demo Project


Further Reading

What’s new in iOS 9.0.1


This update contains bug fixes including:

  • Fixes an issue where some users could not complete setup assistant after updating
  • Fixes an issue where sometimes alarms and timers could fail to play
  • Fixes an issue in Safari and Photos where pausing video could cause the paused frame to appear distorted
  • Fixes an issue where some users with a custom APN setup via a profile would lose cellular data

For information on the security content of this update, please visit this website: