Coder Social home page Coder Social logo

adrianoruiz / cordova-plugin-watchconnectivity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from plbolduc/cordova-plugin-watchconnectivity

0.0 1.0 0.0 25 KB

Simple plugin that establishes iOS Watch Connectivity session with Watch OS and helps exchange of messages between an iPhone Ionic 3.0 hybrid application and its iWatch application.

Objective-C 31.88% Swift 58.64% JavaScript 9.48%

cordova-plugin-watchconnectivity's Introduction

Ionic Cordova Watch Connectivity Plugin

Simple plugin that establishes iOS Watch Connectivity session with Watch OS and helps exchange of messages between an iPhone ionic application and its iWatch application.

Installation

With Ionic cordova-cli

If you are using cordova-cli, install with:

ionic cordova plugin add https://github.com/plbolduc/cordova-plugin-watchconnectivity.git

You can remove the plugin with :

ionic cordova plugin rm cordova-plugin-applewatch-connectivity

*If you are not able to pull from git on Windows try using Git Bash

Xcode configuration

Add the platform to your ionic project

ionic cordova platform add ios

Add applekit to your project in Xcode :

File -> new -> Target -> WatchOs -> Watchkit app

*After that you wont be able to run the command build, because cordova doesn't know about this platform. Instead run the command prepre.

ionic cordova prepare ios

XcodeBuild error : Cordova/CDV.h not found is caused because

To solve it go to your watchkit Extension setting -> Build Setting and remove the string in Objective-c Bridging header

Use from Javascript

Edit src/app/app.component.ts and add the following code

    
    declare var AppleWatchConnectivity: any;

    //...
    
    export class MyApp {
      rootPage:any = HomePage;
    
    
    
      constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
          
        if (platform.is('cordova')) {
            AppleWatchConnectivity.init(function () {
        
                console.log("Init success!");
        
                AppleWatchConnectivity.sendMessage("My first Message",
                    function () {
        
                        console.log("Success callback");
                    },
                    function () {
        
                        console.log("Error callback");
                    })
        
            }, function () {
                //error
                console.log("Init failed!");
            });
        
        } else {
            
            // handle thing accordingly
            console.log("not cordova platform");
        }
      }
    }

Forked repo sample

    
    var didReceiveMessage = function(message){
        var obj = JSON.parse(message);
        alert(obj.message);
    }
    var msgSendSuccess = function() {
        alert("Message send success");
    }
    var failure = function() {
        alert("Error");
    }
    var success = function() {
        sswc.messageReceiver(didReceiveMessage, failure);
        sswc.sendMessage("Message from phone", msgSendSuccess, failure);
    }
    sswc.init(success, failure);

Use from iWatch extension

Swift

//
//  InterfaceController.swift
//
//  Created by Pierre-Luc on 2018-02-14.
//

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController, WCSessionDelegate {
    
    @IBOutlet var testLabel: WKInterfaceLabel!
    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        // Configure interface objects here.
        
        if(WCSession.isSupported()){
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
        
    }
    
    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
        
        if(WCSession.default.isReachable) {
            let message = ["message" : "hellow word"]
            
            WCSession.default.sendMessage(message, replyHandler:
                {(result) -> Void in
                    
                    print("Message sent!")
                    
                    
            }, errorHandler:
                
                
                {(error) -> Void in
                
                    print("Failed!")
                    print(error)
                
                
            })
        }
    }
    
    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    }
    
    func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
        
        let message = message["message"] as? String
        
        testLabel.setText(message)
        
    }
    
}

Credits

Written by Venkatesh D and Vagish M M

Addapted for ionic support by Pierre-Luc Bolduc

More Info

TODO: The plugin is very simple and short without much error handling. This is developed for an immediate need and shall be upgraded to support other platforms with error handling and improved design.

For more information on setting up Cordova see the documentation

For more info on plugins see the Plugin Development Guide

cordova-plugin-watchconnectivity's People

Contributors

dvenkatesh avatar plbolduc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.