Did Ionic already incorporate support for the (default?) Cordova plugins? And will you ever support them?
For example; I know $ionicPlatform.ready() and / or ionic.Platform support some Cordova stuff but how would I implement those and which Cordova features are available through Ionic?
I need some Network connectivity checking in my app before doing tasks (refresh cached data) and want to use the Cordova Network plugin ‘the Angular way’. I’m now injecting a home-made NetworkService which returns Cordova’s navigator.connection.type but I don’t know if Ionic already supports similar functionality.
Is there some documentation on this? How do you guys use Cordova’s plugins in your Ionic Angular apps?
Ionic doesn’t “support” plugins out of the box. That is, it does not provide an interface to the Plugins. With Cordova, the biggest issue is dealing with the ready event. Ionic manages that.
I simple do this for checking the network connection:
It just doesn’t feel right that way. I don’t know. There also isn’t any fallback logic for the moment Cordova is not available (testing in a browser 9 out of 10 times).
I just came up with this:
'use strict';
/**
* bili.services.cordova Module
*
* General Bili Cordova services module
*/
angular.module('bili.services.cordova')
.service('CordovaNetwork', [function() {
// Get Cordova's global Connection object or emulate a smilar one
var Connection = window.Connection || {
'ETHERNET': 'ETHERNET',
'WIFI': 'WIFI',
'CELL_2G': 'CELL_2G',
'CELL_3G': 'CELL_3G',
'CELL_4G': 'CELL_4G',
'CELL': 'CELL',
'EDGE': 'EDGE',
'UNKNOWN': 'unknown'
};
// Get Cordova's global navigator.connection object or emulate one
var networkConnection = navigator.connection || {
type: 'UNKNOWN'
};
return {
isOnline: function () {
var blnReturn = false;
switch (this.getStatus()) {
case Connection.ETHERNET:
case Connection.WIFI:
case Connection.CELL_2G:
case Connection.CELL_3G:
case Connection.CELL_4G:
case Connection.CELL:
blnReturn = true;
break;
}
return blnReturn;
},
getStatus: function () {
return networkConnection.type;
}
};
}]);
Bili is the namespace we use internally for cross-project-usable stuff
I’ve started a project on github called ng-cordova where I’m trying to wrap cordova services in a thoughtful angular way. Any help or feedback is appreciated.
Only thing it’s missing in my opinion is fallback support for testing environments that are not run within the Cordova setup (e.g. plugins should simulate behavior when Cordova is not present).
Above, you can find my CordovaNetwork (com.apache.cordova.network-information) service which is fully working with and without Cordova.
I just finalized a similar service for the com.apache.cordova.dialogs plugin, which is working pretty awesome if I may say so
I created a public Gist on Github to share my progress on this little sideproject with you:
angular.module('yourApp', ['bili.services.cordova'])
.controller('AwesomeController', ['$scope', 'CordovaNetwork', 'CordovaDevice', function ($scope, CordovaNetwork, CordovaDevice) {
$scope.refreshBalance = function () {
if (!CordovaNetwork.isOnline()) {
CordovaDialog.alert(
'What have you done with your internet, son?', // Message
'No Network Connection', // Alert title
'OK' // Button label
);
} else {
// Do whatever you want when you have internet connection
}
}
}]);
I didn’t actually run this code but I hope this gives you an idea on how to use the services.
@Calendee These services or similar once would be an awesome addition to the Ionic Framework. For instance: bower install ionic-cordova-network or bower install ionic-cordova-device
I’m thinking about publishing them to bower myself but I’m a hard-core SVN guy and still need to get used to using GitHub for projects.
Thanks, I’m working on tests and build scripts now. I was thinking about putting each service in it’s own js file ie:
cordova-camera-service.js
cordova-geolocation-service.js
@mwball I like the idea of separating them out, like cordova did with the plugins at the launch of 3.0. Keeps thing modular and prevents carrying the weight of the plugins you don’t want/need.
Good find @Calendee I came across that repo as well. But last updated almost a year ago and an incomplete (probably non-updated, I suppose it was ‘complete’ a year ago but Cordova has developed since) plugin API didn’t make much sense to me. It’s a good starting point but far from production ready imho.