Home

Awesome

3D Touch Cordova plugin

by Eddy Verbruggen

0. Index

  1. Description
  2. Screenshots
  3. Installation
  4. Usage
  5. Static Home Icon Actions
  6. Changelog

1. Description

Add 3D Touch capabilities to your Cordova app:

2. Screenshots

<img src="screenshots/icon-actions-2.PNG" width="235"/>  <img src="screenshots/icon-actions-4.PNG" width="235"/>  <img src="screenshots/link-preview.PNG" width="235"/> 

3. Installation

Latest stable version from npm:

$ cordova plugin add cordova-plugin-3dtouch

Bleeding edge version from Github:

$ cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-3dtouch

ThreeDeeTouch.js is brought in automatically. It adds a global ThreeDeeTouch object which you can use to interact with the plugin.

4. Usage

Check the demo code for all the tricks in the book, or read on for some copy-pasteable samples.

Make sure to wait for deviceready before using any of these functions.

Note that all these functions have optional callbacks, but mostly they're irrelevant, except for the first function here:

isAvailable

You need an iPhone 6S or some future tech to use the features of this plugin, so you can check at runtime if the user's device is supported.

  ThreeDeeTouch.isAvailable(function (avail) {
    // 'avail' is a boolean
    alert("avail? " + avail)
  });

watchForceTouches

You can get a notification when the user force touches the webview. The plugin defines a Force Touch when at least 75% of the maximum force is applied to the screen. Your app will receive the x and y coordinates, so you have to figure out which UI element was touched.

Useful for context menu's, zooming in on images, whatnot.

  ThreeDeeTouch.watchForceTouches(function(result) {
    console.log("force touch % " + result.force); // 84
    console.log("force touch timestamp " + result.timestamp); // 1449908744.706419
    console.log("force touch x coordinate " + result.x); // 213
    console.log("force touch y coordinate " + result.y); // 41
  });

You can also track in JS which was the last element that received an ontouchstart event, remember the timestamp when that happened and correlate that to the timestamp of the force touch. If those are very close to each other you can safely assume the force touch was on that element.

configureQuickActions

When your app starts you can add those fancy Quick Actions to the Home Screen icon. You can configure up to four icons and they are 'cached' until you pass in a new set of icons. So you don't need to do this every time your app loads, but it can't really hurt.

There are two types of icons supported currently iconType and iconTemplate.

iconType

A value from a (case insensitive) fixed list of icons which have been provided by Apple and look great:

Preview icons in Apple's gallery here

iconTemplate

Can be used to provide your own icon. It must be a valid name of an icon template in your Assets catalog.

The type param is the most convenient way to relate the icon to the event you'll receiver when the icon was used to launch your app. So make sure it's unique amongst your icons.

  ThreeDeeTouch.configureQuickActions([
    {
      type: 'checkin', // optional, but can be used in the onHomeIconPressed callback
      title: 'Check in', // mandatory
      subtitle: 'Quickly check in', // optional
      iconType: 'Compose' // optional
    },
    {
      type: 'share',
      title: 'Share',
      subtitle: 'Share like you care',
      iconType: 'Share'
    },
    {
      type: 'search',
      title: 'Search',
      iconType: 'Search'
    },
    {
      title: 'Show favorites',
      iconTemplate: 'HeartTemplate' // from Assets catalog
    }
  ]);

onHomeIconPressed

When a home icon is pressed, your app launches and this JS callback is invoked. I found it worked reliable when you use it like this (you should recognize the type params used previously):

  document.addEventListener('deviceready', function () {
    ThreeDeeTouch.onHomeIconPressed = function (payload) {
      console.log("Icon pressed. Type: " + payload.type + ". Title: " + payload.title + ".");
      if (payload.type == 'checkin') {
        document.location = 'checkin.html';
      } else if (payload.type == 'share') {
        document.location = 'share.html';
      } else {
        // hook up any other icons you may have and do something awesome (e.g. launch the Camera UI, then share the image to Twitter)
        console.log(JSON.stringify(payload));
      }
    }
  }, false);

enableLinkPreview

UIWebView and WKWebView (the webviews powering Cordova apps) don't allow the fancy new link preview feature of iOS9. If you have a 3D Touch enabled device though, you sometimes are allowed to force press a link and a preview pops up (see the screenshot above). If you want to enable this feature, do:

  ThreeDeeTouch.enableLinkPreview();

disableLinkPreview

To disable the link preview feature again, do:

  ThreeDeeTouch.disableLinkPreview();

5. Static Home Icon Actions

The configureQuickActions function above can add dynamic icon actions to your app, but what if you want to have actions immediately after installation from the AppStore, before opening your app?

That's where static icons come in, which need to be configured in your app's .plist file. Let's say you want these actions:

<img src="screenshots/icon-action-2-static.jpg" width="360" height="223"/>

Then add this anywhere in the .plist:

	<key>UIApplicationShortcutItems</key>
	<array>
		<dict>
			<key>UIApplicationShortcutItemIconFile</key>
			<string>Eye</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>Eye from plist</string>
			<key>UIApplicationShortcutItemSubtitle</key>
			<string>Awesome subtitle</string>
			<key>UIApplicationShortcutItemType</key>
			<string>eyefromplist</string>
		</dict>
		<dict>
			<key>UIApplicationShortcutItemIconType</key>
			<string>UIApplicationShortcutIconTypeCompose</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>Compose</string>
			<key>UIApplicationShortcutItemType</key>
			<string>compose</string>
		</dict>
	</array>

UIApplicationShortcutItemIconFile

The second action uses the built-in UIApplicationShortcutIconTypeCompose icon (which is the same as the Compose icon you'd get when using the configureQuickActions), but the first one uses a custom icon: Eye. This expects an Eye.png file in your app's bundle. According to Apple's docs this needs to be a single color square 35x35 icon, but that will look pixelated on retina devices, so go ahead and use a 70x70 or 105x105 icon if you please.

In Xcode just drag the icon to the Resources folder. If you're using Telerik Platform you can add it to the App_Resources/iOS folder. That's where the .plist is stored as well.

UIApplicationShortcutItemTitle / UIApplicationShortcutItemSubtitle

You can guess what those do by looking at the screenshot, right?

Note that you can localize these by opening Xcode, and adding a InfoPlist.strings file to the Resources folder. Then mark it as Localizable in the Utilities window and add translations for the appropriate languages. Details here.

If you're using Telerik Platform you can manually upload this plugin and tweak the plugin's contents. See the commented section in plugin.xml about static icon localization.

UIApplicationShortcutItemType

This is the same as the type param of configureQuickActions, so it's what you'll receive in your onHomeIconPressed as payload.type. Just do something cool with that info.

6. Changelog