All Projects → AlexisLeon → react-native-watch

AlexisLeon / react-native-watch

Licence: MIT license
React Native bindings for WatchKit

Programming Languages

objective c
16641 projects - #2 most used programming language
javascript
184084 projects - #8 most used programming language
java
68154 projects - #9 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to react-native-watch

SmogWatch
watchOS app for checking air pollution levels, created for my blog post series
Stars: ✭ 34 (+21.43%)
Mutual labels:  watchkit
Ios Samples
Xamarin.iOS sample apps
Stars: ✭ 1,501 (+5260.71%)
Mutual labels:  watchkit
WatchKitTimePicker
⏱ A Time Picker data source for WatchKit that mirrors the behavior of UIDatePicker.
Stars: ✭ 37 (+32.14%)
Mutual labels:  watchkit
WatchActivityIndicator
An Apple Watch activity indicator
Stars: ✭ 23 (-17.86%)
Mutual labels:  watchkit

React Native Watch

Travis npm version npm dm npm dt

This is a beta release

Requirements:

  • react-native >= 0.41.2 for iOS

Connect your React Native App with WatchKit.

Android Wear support coming soon..

Getting started

yarn add react-native-watch

Link the library to your project automatically by using:

react-native link react-native-watch

Or manually, by adding node_modules/react-native-watch/RNWatch.xcodeproj to your project and adding libRNWatch.a to Build PhasesLink Binary With Libraries

Once linked to your project, modify AppDelegate.h:

#import <UIKit/UIKit.h>
+ #import <WatchConnectivity/WatchConnectivity.h>

+ @class RNWatch;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;
+ @property (nonatomic, strong) RNWatch *watchBridge;
+ @property (nonatomic, strong) WCSession *session;

@end

And modify AppDelegate.m

#import "AppDelegate.h"
+ #import "RNWatch.h"

...

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...
+  self.watchBridge = [RNWatch shared];
+  self.session = self.watchBridge.session;

  return YES;

Usage

Use the examples below to start sending data between React Native and WatchOS (Swift)

ES5

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

ES6

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

Messages

Receive messages

Subscribe and unsubscribe to all messages.

// Subscribe
componentDidMount() {
  this.messageSubscription = watch.subscribeToMessages(
    (err, message) => {
      if (err) console.error('Error receiving message', err)
      else {
        ...
      }
    }
  )
}

// Unsubscribe
componentWillUnmount() {
  // messageSubscription -> remove function waiting to be called
  this.messageSubscription()
}

Send Message

Send messages and receive a response

watch.sendMessage(data, (err, res) => {
  if (err) console.error('Error sending message to watch', err)
  else {
    console.log('Watch received the message sent')
    if (res) {
      console.log('Watch replied', res)
    }
  }
})

Testing

yarn run test

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].