• Stars
    star
    264
  • Rank 155,103 (Top 4 %)
  • Language
    Objective-C
  • Created over 9 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cordova / PhoneGap Apple Watch Plugin for Apache Cordova

Apple Watch Plugin for Apache Cordova npm version

Cordova / PhoneGap Plugin for the Apple Watch (WatchKit) to allow communication between a Cordova app and an Apple WatchKit Extension (and vice versa).

Simplified overarching diagram for message passing:

You will need to write your own WatchKit Extension and WatchKit app with native code. It is not possible to run a Cordova app directly on the Watch, as there is no support for a WebView and the WatchKit code must reside in the WatchKit Extension. This plugin provides various methods of communication between a Cordova iPhone app and the WatchKit Extension / app.

For more information on developing your WatchKit Extension / app, please see the WatchKit Programming Guide.

Supported methods of communication:

  • Message passing - in memory and lightweight json object message passing over named queues between a Cordova app and a WatchKit Extension (2-way)
  • Local notifications - sending notifications directly from a Cordova app to an Apple Watch
  • User defaults - persisting user data accessible by both a Cordova app and a WatchKit Extension

Please note that you cannot force a Cordova app to open from the Apple Watch - this is a limitation set by Apple.

Install

Latest published version on npm (with Cordova CLI >= 5.0.0)

cordova plugin add cordova-plugin-apple-watch

Latest version from GitHub

cordova plugin add https://github.com/leecrossley/cordova-plugin-apple-watch.git

You do not need to reference any JavaScript, the Cordova plugin architecture will add a applewatch object to your root automatically when you build.

Message passing

Some success and error handlers may be omitted. This is catered for in the interface function argument orders.

init

Initialises the Apple Watch two way messaging interface. This must be called and the success handler fired before sendMessage can be used.

applewatch.init(function successHandler(appGroupId) {}, errorHandler);

The successHandler is called with one arg appGroupId that was used in initialisation. The app bundleId will be used for identification by default, prefixed by "group.".

You can supply your own Application Group Id with the optional appGroupId argument, this should be in the format "group.com.company.app":

applewatch.init(successHandler, errorHandler, appGroupId);

sendMessage

Sends a message object to a specific queue (must be called after successful init).

Used to send strings or json objects to the Apple Watch extension. Json objects are automatically stringified.

The value of queueName must match a value used in a corresponding listener in Swift or Objective-C within the watch app using the method MMWormhole.listenForMessageWithIdentifier in order for the watch to receive the sent message.

applewatch.sendMessage(message, queueName, successHandler, errorHandler);

addListener

Adds a listener to handle a message object received on a specific queue (must be called after successful init).

Used to handle strings or json objects received from the Apple Watch extension. Json objects are automatically parsed.

The value of queueName must be used in a corresponding MMWormhole.passMessageObject method in Swift or Objective-C within the watch app in order for the Cordova app to receive a message.

applewatch.addListener(queueName, messageHandler);

removeListener

Removes a listener for a specific queue.

applewatch.removeListener(queueName, successHandler, errorHandler);

purgeQueue

Use with caution: removes all messages on a queue.

applewatch.purgeQueue(queueName, successHandler, errorHandler);

purgeAllQueues

Use with extreme caution: removes all messages on all queues.

applewatch.purgeAllQueues(successHandler, errorHandler);

Message passing examples

Example to send a message "test" to the "myqueue" queue and get handled, where "com.yourcompany" is the App ID and "group.com.yourcompany" is the App Group.

Initialise message passing (Cordova app, js)

applewatch.init(function (appGroupId) {
    // success, messages may now be sent or listened for
}, function (err) {
    // an error occurred
},
"group.com.yourcompany");

Send a message (Cordova app, js)

// assumes a previously successful init call (above)

applewatch.sendMessage("test", "from_phone_queue");

Listen for messages (Cordova app, js)

// assumes a previously successful init call (above)

applewatch.addListener("from_watch_queue", function (message) {
    // handle your message here
});

Initialise message passing (WatchKit extension, swift)

// assumes your WatchKit extension references Wormhole.h in a Bridging-Header.h file

let watchConnectivityListeningWormhole = MMWormholeSession.sharedListeningSession();
watchConnectivityListeningWormhole.activateSessionListening();

let wormhole = MMWormhole(applicationGroupIdentifier: "group.com.yourcompany", optionalDirectory: nil, transitingType: .SessionContext);

Send a message (WatchKit extension, swift)

// assumes wormhole is initialised (above)

wormhole.passMessageObject("titleString", identifier: "from_watch_queue")

Listen for messages (WatchKit extension, swift)

// assumes wormhole is initialised (above)

watchConnectivityListeningWormhole.listenForMessageWithIdentifier("from_phone_queue", listener: { (messageObject) -> Void in
    if let message: AnyObject = messageObject {
        // handle your message here
    }
})

More information regarding the MMWormhole component used in message passing can be found here.

Notifications

registerNotifications

Requests permission for local notifications if you want to utilise the short-look / long-look notification interface. This must be called and the success handler fired before sendNotification will work correctly.

applewatch.registerNotifications(successHandler, errorHandler);
  • successHandler is called with true if the permission was accepted
  • errorHandler is called with false if the permission was rejected

sendNotification

Sends a local notification directly to the Apple Watch (should be called after successful registerNotifications).

Used to display the Apple Watch short-look / long-look notification interface, using UILocalNotification. If the user continues to look at the notification, the system transitions quickly from the short-look interface to the long-look interface.

var payload = {
    "title": "Short!",
    "category": "default",
    "body": "Shown in the long-look interface to provide more detail",
    "badge": 1
};

applewatch.sendNotification(successHandler, errorHandler, payload);
  • title - shown in the short-look interface as a brief indication of the intent of the notification
  • category - defines the notification interface to show and action buttons (if any)
  • body - shown in the long-look interface to provide more detail
  • badge - app icon badge number

NB: This notification will also appear on the iPhone if the app is running in a background mode.

User defaults

sendUserDefaults

Allows persistence of user default data (single property key/value object) that can be retrieved by the WatchKit extension.

applewatch.sendUserDefaults(successHandler,
    errorHandler, { "myKey": "myValue" }, appGroupId);

The app bundleId will be used for identification by default, prefixed by "group." if appGroupId is not supplied.

For completeness, here's how you could retrieve the value in your WatchKit extension (swift):

let userDefaults = NSUserDefaults(suiteName: "group.com.yourcompany")

var myValue: String? {
    userDefaults?.synchronize()
    return userDefaults?.stringForKey("myKey")
}

getUserDefaults

Allows retrieval of user default data.

applewatch.getUserDefaults(successHandler, errorHandler, "myKey", appGroupId);

The app bundleId will be used for identification by default, prefixed by "group." if appGroupId is not supplied.

Live demo

See this plugin working in a live app: sprint.social

Platforms

iOS 8.2+ only. However, you can include this plugin and set a lower minimum iOS version. For example setting a deployment target of 8.0 will not cause any errors with the plugin. Similarly, there will be no errors if a user is using the Cordova app without a paired watch.

watchOS 1 & 2.

License

MIT License

More Repositories

1

cordova-plugin-native-transitions

Cordova / PhoneGap Native Transitions Plugin for Apache Cordova >= 3.0.0
Objective-C
143
star
2

cordova-plugin-social-message

Cordova / PhoneGap Social Message Plugin for Apache Cordova >= 3.0.0
Objective-C
121
star
3

cordova-plugin-touchid

Cordova / PhoneGap Touch ID Plugin for Apache Cordova >= 3.0.0
Objective-C
100
star
4

cordova-plugin-shake

Cordova / PhoneGap Plugin to detect when a physical device performs a shake gesture
JavaScript
94
star
5

cordova-plugin-game-center

Cordova / PhoneGap Game Center Plugin for Apache Cordova >= 3.0.0
Objective-C
78
star
6

cordova-plugin-transport-security

Cordova / PhoneGap Plugin to bypass the iOS 9 App Transport Security (NSAppTransportSecurity)
74
star
7

cordova-plugin-pedometer

Cordova / PhoneGap Core Motion Pedometer Plugin for Apache Cordova >= 3.0.0
Java
60
star
8

cordova-plugin-jailbreak-detection

Cordova / PhoneGap Jailbreak Detection Plugin for Apache Cordova >= 3.0.0
Objective-C
37
star
9

cordova-plugin-background-task

Cordova / PhoneGap Background Task Plugin for Apache Cordova >= 3.0.0
Objective-C
31
star
10

cordova-plugin-directions

Cordova / PhoneGap Map Directions Plugin for Apache Cordova >= 3.0.0
Java
29
star
11

hexo-generator-cname

Github pages CNAME generator plugin for Hexo
JavaScript
24
star
12

cordova-plugin-dynamic-update

Cordova / PhoneGap Dynamic Update Plugin
Java
20
star
13

grunt-timer

Time the execution of each of your grunt tasks automatically
JavaScript
14
star
14

cordova-plugin-progress

Cordova Plugin for Progress HUD Notifications (via KVNProgress)
Objective-C
11
star
15

cordova-plugin-boot-launcher

Cordova / PhoneGap Boot Launcher Plugin for Apache Cordova >= 3.0.0
Java
9
star
16

hexo-generator-robotstxt

Basic robots.txt generator plugin for Hexo
JavaScript
8
star
17

isNumeric

Determine if a JavaScript object is numeric
JavaScript
8
star
18

replaceall

Replace all instances in a JavaScript string
JavaScript
8
star
19

cordova-plugin-datetime-picker

Cordova / PhoneGap Date Time Picker Plugin for Apache Cordova >= 3.0.0 (Windows Phone 8)
C#
8
star
20

imageurl-base64

Node package to convert an image from a url to base64
JavaScript
7
star
21

dotject

Dot notation to object
JavaScript
6
star
22

functionaljs-docs

Markdown documentation for functional.js
4
star
23

grunt-pogo

Compile your PogoScript to JavaScript with Grunt
JavaScript
4
star
24

cordova-plugin-date-input

Cordova / PhoneGap Date Input Plugin for Apache Cordova >= 3.0.0
JavaScript
3
star
25

froute

A simple and powerful routing for node with expressive matching
JavaScript
3
star
26

appsassin

Game developed for #HackManchester. "Appsassin" – Travel the world. Meet new people. Kill them.
JavaScript
3
star
27

Real-Simple-Server

Spin up a real simple http server with node
2
star
28

PhoneStub

Facilitates the testing and running of Cordova (PhoneGap) based applications in a desktop or headless browser.
JavaScript
2
star
29

atom-dotject

Dot notation to object for https://atom.io/
CoffeeScript
2
star
30

froute-picker

Picking and matching module for froute
JavaScript
2
star
31

Mobile-Starter

Mobile Starter Template for the Wednesday Widget Workshop (WWW)
2
star
32

health-heatmap

2
star
33

grunt-pause

Allow pausing between grunt tasks
JavaScript
2
star
34

mumford

I will wait, I will wait for you
JavaScript
2
star
35

nativescript-app-rss-reader

RSS Reader demo app built with NativeScript
JavaScript
2
star
36

The-28-Minute-App

Dev Led Training Code
1
star
37

includes

Include html files within html files with Node.js
JavaScript
1
star
38

christmas-ribbon

Christmas ribbon css for the top right of a webpage
1
star
39

Retina

Mobile OCR App for NICE Evidence (WWW)
JavaScript
1
star
40

blurchat-site

BlurChat website
CSS
1
star
41

shake.js

Shake gesture detection in PhoneGap / Cordova (migrated from gist 4078996)
JavaScript
1
star