Site Slowness

Dear Friends,

ever since the iPhone 6s was launched last week, this site has been experiencing explosive traffic spikes and attracts more visitors than ever. While that’s very reassuring, it sadly is much more than my current hosting environment can withstand.

Please bear with me as I move the site to a different infrastructure.

Thank you for your patience!



How to display Live Photos in a view controller

Live Photo

Live Photos are a brand new kettle of fish: they’re not a UIImage (nor an NSImage), and they’re not a video resource either. To make use of them, Apple gave us a few new classes in iOS 9: PHLivePhoto and PHLivePhotoView. In addition there’s the the PHLivePhotoViewDelegate protocol we can utilise.

Let’s see how to grab a Live Photo from the user’s camera roll, check if it is in fact a Live Photo, and then display it in a Live Photo View. I’ll also describe how to animate the image programmatically.

Continue reading How to display Live Photos in a view controller

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 How to track finger position and touch force in iOS 9

How to create alert views and action sheets in iOS 8

Screen Shot 2015 09 28 at 23 37 12

Up until iOS 7 we could use the UIAlertView and UIActionSheet classes to bring up alerts and action sheets respectively. As of iOS 8 those classes are deprecated. Although they still work, we’re encouraged to use the UIActionController class instead. Here’s how to use it.

The main difference is that both of the old classes have been converged into one, so now we simply tell the alert controller how we want to bring up the information.

While the old classes used a delegate protocol so we could react to the outcome of user interactions, the UIActionController class uses completion blocks, making our coding efforts a little easier. All we have to do is create an action, specify things like title, subtitle and appearance, and add a block that shall be executed upon completion.

Here are three examples.

Continue reading How to create alert views and action sheets in iOS 8

3D Touch in iOS 9, Part 4: Adding Static Shortcut Items


The array of shortcut items can also be added to our app’s Info.plist. The result is almost the same as generating this array in code (as discussed in my previous article): the difference is that static shortcuts are available as soon as the app is installed, even if the user hasn’t opened it yet. With dynamic shortcuts, the app needs to be opened at least once for the shortcuts to be populated.

To get started, select your app’s Info.plist file in Xcode. If you’ve never used the plist editor before, you’re in for a little culture shock (because it’s neither fun nor intuitive to use). Here’s what you do step by step.

Add a new row to the file by right-clicking at the bottom of the list. Call the new row UIApplicationShortcutItems and turn it into an array. Notice that little triangle at the beginning of the row. Click to open it. Now hover over your new row and click on the plus icon that appears. This will create a new item in our array (whose name you can’t change). Hover over Item 0 and turn it into a dictionary, then open it with the disclosure triangle that has popped up.

This item will define your first shortcut. Click the plus icon in Item 0 to add a new row for each key and enter the following (or as many as you need):

  • UIApplicationShortcutItemTitle
  • UIApplicationShortcutItemSubtitle
  • UIApplicationShortcutItemType
  • UIApplicationShortcutItemIconFile

Give each of these an appropriate value until you get something like this:

Screen Shot 2015-09-28 at 07.30.34

Rinse and repeat for as many static shortcuts as you need. You have several other keys at your disposal, read all about those in Apple’s Info Plist Documentation.

Mixing with Dynamic Shortcuts

You can use both dynamic and static shortcuts if you like. In my previous article about dynamic shortcuts we’ve already seen how these are created. In order not to overwrite existing static shortcuts though, we need to add our dynamic shortcuts to the existing array we’ve just created rather than wipe it out (as we did before).

We can do this by using the arrayByAddingObjectsFromArray method, combining both arrays:

- (void)createItemsWithIcons {
    // add this array to the potentially existing static UIApplicationShortcutItems
    NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
    NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:items];
    [UIApplication sharedApplication].shortcutItems = updatedItems;


We react to static shortcuts just like we did to dynamic ones. I’ve explained how to do it in my previous article.

Demo Project

I’ve added all this static goodness to my demo project on GitHub:

3D Touch in iOS 9, Part 3: Adding Dynamic Shortcut Items


We can define shortcut items that appear when the user presses hard on the app icon from the home screen. If available, the springboard blurs and only leaves our app icon and the shortcut actions visible.

These items can be generated in two ways: via code, and via entries in our app’s Info.plist file. In this article I’ll show you how to create dynamic shortcut items in code. We’ll talk about Static Shortcut Items in my next article.

The difference between this approach and the Info.plist entries is that a user needs to have launched our app at least once for our shortcuts to become available. This is also true for changes to these items after an app upgrade. Another difference is that dynamic items can be updated, while static ones can not (unless we deploy a new version to iTunes Connect).

Shortcut Items without Icons

Shortcut items can be generated without icons with relative ease, as instances of the UIApplicationShortcutItem class. Here’s how to do it in your AppDelegate:

- (void)createDynamicShortcutItems {
    // create several (dynamic) shortcut items
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"Item 1" localizedTitle:@"Item 1"];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"Item 2" localizedTitle:@"Item 2"];
    UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc]initWithType:@"Item 3" localizedTitle:@"Item 3"];
    // add all items to an array
    NSArray *items = @[item1, item2, item3];
    // add the array to our app
    [UIApplication sharedApplication].shortcutItems = items;

Call this method in applicationDidFinishLaunchingWithOptions. We’ll talk about how to react to these items in a moment.

Shortcut Items with your own Icons

Alternatively we can add swanky icons to these actions, as well as subtitles if we like. The method for this is another init method for the same class as above, taking more parameters. Icons are simple PNG files in 35×35 pixels (or 2x/3x equivalents of those dimensions). Add them to your asset catalogue and instantiate them like this:

This content is for members only.

Reacting to Shortcut Items

  1. There are two things that can happen when a user selects one of our shiny new shortcut items:
    either the app launches for the first time
  2. or the app is still open in the background

To cover both eventualities, we need to add two things to our AppDelegate.

The first is a check in applicationDidLaucnhingWithOptions to tell us how the app needs to be presented here. Think of the camera app for example: if the user selects the “selfie” option, the front camera needs to be active. Or if he wants to take a normal picture, the back camera needs to be active. We don’t want them to press other options, otherwise they might as well launch our app without a shortcut.

To make it simple here, I’ll print a log message and the title of our shortcut:

This content is for members only.

Further Reading