/react-native-watch-connectivity

Allow your iWatch app and react native app to communicate

Primary LanguageJavaScriptMIT LicenseMIT

WARNING: This library is a work in progress

React Native Watch Connectivity

Communicate with your apple watch apps over the react native bridge.

Note: This library does not allow you to write your iWatch apps in React Native but rather allows your RN iOS app to communicate with a watch app written in Obj-C/Swift.

Demo

The featured screenshot is from the demo app. To get the demo app going:

git clone https://github.com/mtford90/react-native-watch-connectivity.git
cd react-native-watch-connectivity
npm install
cd ios
pod install
open buff.xcworkspace

And then run the app!

Install

npm install react-native-watch-connectivity

Then add node_modules/react-native-watch-connectivity/RNWatch.xcodeproj to your project and ensure that libRNWatch.a is present in the Link Binary With Libraries build phase

Usage

ES6

import * as watch from 'react-native-watch-connectivity'

ES5

var watch = require('react-native-watch-connectivity')

Reachability

// Monitor reachability
const unsubscribe = watch.subscribeToWatchReachability(watchIsReachable => {
    this.setState({watchIsReachable})
})

// Get current reachability
watch.getReachability(watchIsReachable => {
  // ...
})

Watch State

// Monitor watch state
const unsubscribe = watch.subscribeToWatchState(watchState => {
    console.log('watchState', watchState) // NotActivated, Inactive, Activated
})

// Get current watch state
watch.getWatchState(watchState => {
    console.log('watchState', watchState) // NotActivated, Inactive, Activated
})

Messages

Send Message

Send messages and receive replies

watch.sendMessage({text: "Hi watch!"}, (err, replyMessage) => {
    console.log("Received reply from watch", replyMessage)
})

Receive Message

Recieve messages and send responses

const unsubscribe = watch.subscribeToMessages((err, message, reply) => {
    if (!err) reply({text: "message received!"})
})

Message Data

TODO: Undocumented & partially implemented

Files

Send Files

const uri = 'file://...' // e.g. a photo/video obtained using react-native-image-picker

watch.transferFile(uri).then(() => {
  // ...
}).catch(err => {
  // ... handle error
})

Receive Files

TODO: Not implemented or documented

User Info

const unsubscribe = watch.subscribeToUserInfo((err, info) => {
    // ...
})
watch.sendUserInfo({name: 'Mike', id: 5})
watch.getUserInfo().then(info => {
    // ...
}).catch(err => {
    // ...
})