3D Touch Cordova plugin
Add 3D Touch capabilities to your Cordova app:
- Quick Action for Home Screen icons. Static and Dynamic.
- Enable Link preview for external links.
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.
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:
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.
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.
You can also track in JS which was the last element that received an
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.
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
A value from a (case insensitive) fixed list of icons which have been provided by Apple and look great:
- iOS 9.0: Compose, Play, Pause, Add, Location, Search, Share
- iOS 9.1 added these: Prohibit, Contact, Home, MarkLocation, Favorite, Love, Cloud, Invitation, Confirmation, Mail, Message, Date, Time, CapturePhoto, CaptureVideo, Task, TaskCompleted, Alarm, Bookmark, Shuffle, Audio, Update
Can be used to provide your own icon. It must be a valid name of an icon template in your Assets catalog.
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.
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):
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:
To disable the link preview feature again, do:
5. Static Home Icon Actions
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:
Then add this anywhere in the
UIApplicationShortcutItemsUIApplicationShortcutItemIconFileEyeUIApplicationShortcutItemTitleEye from plistUIApplicationShortcutItemSubtitleAwesome subtitleUIApplicationShortcutItemTypeeyefromplistUIApplicationShortcutItemIconTypeUIApplicationShortcutIconTypeComposeUIApplicationShortcutItemTitleComposeUIApplicationShortcutItemTypecompose
The second action uses the built-in
(which is the same as the
Compose icon you'd get when using the
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
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.
This is the same as the
type param of
configureQuickActions, so it's what you'll receive in your
payload.type. Just do something cool with that info.
- 1.3.5 Home icons are now WKWebView compatible. Previously your app would crash. See #12.
- 1.3.4 Increased the wait time for
onHomeIconPressedfrom 5 to 15 secs. See #4.
- 1.3.3 Compatibility of 'home icon cold-starts' with Meteor, see #4.
- 1.3.2 Compatibility with Cordova-iOS 4.
- 1.3.1 Added
timestampto the response of
- 1.3.0 You can now receive notifications when the user applies a 'Force Touch' on the webview.
- 1.2.2 Documentation on how to localize the title and subtitle of your static icons.
- 1.2.1 Documentation on how to add static icons to your app.
- 1.2.0 iOS 9.1 added a lot of new iconTypes to choose from. Thanks #2!
- 1.1.0 Found a solid way to deal with timing when to call into
onHomeIconPressed. Should always work now, even on coldstart.
- 1.0.1 Increased the timeouts a bit, so there is a better chance
onHomeIconPressedgets called on coldstart. Thanks #1.
- 1.0.0 Initial release (untagged)