As competition in mobility grows and hardware race becomes less of an issue, usability sets the stage for a new demand model. Simplification. A device as hi-tech as an iPhone has to be super easy-to-use, friendly, and accessible as a match box. This means it has to predict your needs, work fast, and be as handy as possible.

With wide-screen phones being a staple of today’s mobility, making use of lesser motion became a thing of interest for mobile developers. The D Touch technology allows just that. With a limited range of motion, for the first time, it utilizes the Z-axis to trigger certain functions. Neglecting that would’ve been a mistake and miss-out, so let’s see how to introduce the 3D Touch technology in a generic application.

At the top of the year, upon the release of iPhone 7, we listed 3D Touch among one of the key features of the newest device. Time to dig deeper and figure out what it takes to add the 3D Touch functionality to an iOS app.

3D Touch Utility

The 3D Touch technology was first introduced on the iPhone 6s and 6s+. Devices supporting 3D Touch are equipped with a tap force sensitive display, measuring the pressure on the screen. The 3D Touch technology allows users to press an app icon on the Home screen and get a quick access to some functionality presented in the app. Also, within an app, a user can get access to some features.

From iOS 9, Apple made 3D Touch APIs available:

Home Screen Quick Action API

UIKit peek and pop API

Web view peek and pop API

UITouch force properties

In order to find out whether a device supports the 3D Touch technology, you have to read out the forceTouchCapability values. While the app is working, a user can turn off 3D Touch, so this value has to be checked in the traitCollectionDidChange delegate method.

- (void)traitCollectionDidChange:( UITraitCollection *)previousTraitCollection { if ( self .traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable ) { NSLog (@ "3D Touch is available" ); } else { NSLog (@ "3D Touch is not available on this device" ); } }

3D Touch Home Screen Quick Actions

There are two types of Home Screen Quick Actions: dynamic and static.

Static actions are defined in the Info.plist file within the UIApplicationShortcutItems array.

Dynamic actions have to be added to the UIApplication application object in the shortcutItems property. You can use two methods for creation:

Method 1

init (type: String , localizedTitle: String , localizedSubtitle: String ?, icon: UIApplicationShortcutIcon ?, userInfo: [ AnyHashable : Any ]? = nil )

This method creates a Home screen dynamic quick action with a header, optional subheader, optional icon, and optional user info dictionary.

Method 2

convenience init (type: String , localizedTitle: String )

Creates a Home screen dynamic quick action with a header but with no icon.

Quick Actions Handler

func application(application: UIApplication,

performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {

let didHandle: Bool = /* handle the quick action using shortcutItem */

completionHandler(didHandle)

}

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

var performAdditionalHandling = true

if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey]

as? UIApplicationShortcutItem {

/* handle the quick action using shortcutItem */

performAdditionalHandling = false

}

return performAdditionalHandling

}

UIKit peek and pop API

This API is used for content preview (quick) and further transition to it. New methods in UIViewController for ViewController registration and registration cancellation allow notifications as to whether it is going to be used by 3D Touch. Additionally added are new protocols for 3D Touch support.

ViewController registration:

-(id)registerForPreviewingWithDelegate:(id)delegate sourceView:(UIView *)sourceView;

Peek:

- (UIViewController *)previewingContext: (id)previewingContext viewControllerForLocation:(CGPoint)location {

// check if we're not already displaying a preview controller

if ([self.presentedViewController isKindOfClass:[PreviewViewController class]]) {

return nil;

}

// shallow press: return the preview controller here (peek)

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"PreviewView"];

return previewController;

}

Commit:

- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {

// deep press: bring up the commit view controller (pop)

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

UIViewController *commitController = [storyboard instantiateViewControllerWithIdentifier:@"CommitView"];

[self showViewController:commitController sender:self];

// alternatively, use the view controller that's being provided here (viewControllerToCommit)

}

In preview, you can also add UIPreviewAction and UIPreviewActionGroup

UIPreviewAction *action1 = [ UIPreviewAction actionWithTitle:@ "Action 1" style: UIPreviewActionStyleDefault handler:^( UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { NSLog (@ "Action 1 triggered" ); }];

NSArray *actions = @[action1, action2, action3];

UIPreviewActionGroup *group1 = [ UIPreviewActionGroup actionGroupWithTitle:@ "Action Group" style: UIPreviewActionStyleDefault actions:actions]; NSArray *group = @[group1];

The True Potential of 3D Touch

As developers are getting to know the benefits of the 3D technology, it becomes clear that it will become a staple. Thus, this contribution to unlocking the true potential of 3D Touch development in Shakuro.

We are looking forward to exploring this productive feature further to make applications more usable, fast, and robust. In-app use of the technology has all the chances to tackle the way swiping has ingrown into our daily device usage.