# WeFitter HealthKit Cordova Plugin

### 1. Installation
You should install the cordova plugin on your cordova project to use the plugin:


```bash
cordova plugin add com.wefitter.plugins.healthkit --variable HEALTH_READ_PERMISSION='App needs read access' --variable HEALTH_WRITE_PERMISSION='App needs write access'
```

The plugin will be saved on `window.plugins.wefitterhealthkit` variable (on a device environment, not browser!).

### 2. Usage of WeFitter Plugin
#### 2.1. Be sure plugin are enabled
Before the library `run()` execution, you should be sure that the plugin are available. Usage:

```
window.plugins.wefitterhealthkit.available(function(yes) {
  var libraryWorking = true;
  console.log("window.plugins.wefitterhealthkit available");
  window.plugins.wefitterhealthkit.setAPIToken('test');
  window.plugins.wefitterhealthkit.setAuthToken('wf5b87108d2d7ac91789342818629500');
}, function(no) {
  console.log("window.plugins.wefitterhealthkit not available");
});
```

Remember that depending of the platform, maybe you should use `document.addEventListener("deviceready", function () {[...]});` before try to access to the cordova plugin.


#### 2.2. Configure Auth
Before `run()`, you will need to configure auth with `setAPIToken` and `setAuthToken`, for example:

```
window.plugins.wefitterhealthkit.setAPIToken('test');
window.plugins.wefitterhealthkit.setAuthToken('wf5b87108d2d7ac91789342818629500');
```

You can change it dinamically.

#### 2.3. Run and send healthkit data to WeFitter

If the `available()` function result are true, you can go and run without problem `run()` to retrieve and send the healthkit data to WeFitter API. It will send the data only one time, on background and async.

Example of usage:

```
window.plugins.wefitterhealthkit.run({}, function(yes) {
  console.log("healthkit run() working");
}, function(no) {
  console.log("healthkit run() not working:");
  console.log(no);
});
```
##### Success messages
- "data sent correctly": Plugin worked perfectly

##### Error messages
- "healthkit permissions error": The user doesn't accept the healthkit permissions
- "data send failed": Server connection error
- "auth variables are undefined": you don't used the auth functions to set the auth variables



##### Option: backendConfig
If you use `backendConfig: true` the plugin will retrieve before the communication with healthkit the healthkit config to send customized data to the server.

Example:

```
window.plugins.wefitterhealthkit.run({backendConfig: true}, function(yes) {
  console.log("sent data correctly");
}, function(no) {
  console.log("send data failed");
  console.log(no);
});
```