salbahra / cordova-plugin-networkinterface

Provide a method for Cordova applications to retrieve the device IP Address.
http://albahra.com/projects
72 stars 51 forks source link

Feature: Add subnet support for iOS and Android #24

Closed dharders closed 7 years ago

dharders commented 7 years ago

This PR adds subnet support (i.e. netmask info) for both iOS and Android. (maybe more platforms in a future PR)

The subnet info is provided to the onSuccess() callback as a separate argument (string value). Eg

function onSuccess(ip, subnet) { }

This was simply following your comment:

I think to avoid a soup of callbacks we can probably return the netmask in the IP request as a seperate argument in the callback.

The code can easily return additional information now using separate arguments if you choose to in the future. However, depending on how much additional information you want to provide in the future I'd maybe suggest providing an object of additionalInfo eg { subnet: '255.255.255.0', gateway: '192.168.1.1', broadcast: '...' } etc to avoid "argument hell". But I'll leave this future decision to you.

I've tested this plugin on an iPhone 6 plus, iPad 2017, Galaxy Note II and Moto G3 and all work successfully. iOS and Android behave a little differently, where iOS seems to give the Carrier info when WiFi is on, but Android will not give the Carrier info until you turn WiFi off. i.e. Android only provides the currently used radio (but I believe this was the behavior before this PR anyway). The onError() callback is called when the respective radio is disabled (i.e. previous behaviour). i.e. Airplane mode, WiFi off, No SIM card etc

To test this PR on your own device, prior to merging, run the following commands:

cordova create subnetTest com.yourcompany.app
cd subnetTest
cordova platform add ios
cordova platform add android
cordova plugin add https://github.com/dharders/cordova-plugin-networkinterface.git

Then simply add to /www/js/index.js in the onDeviceReady function()

networkinterface.getWiFiIPAddress(
    function (ip, subnet) { print('WiFi: ' + ip + ' : ' + subnet) }, 
    function (err) { print('WiFi Err: ' + ip + ' : ' + subnet) }
);
networkinterface.getCarrierIPAddress(
    function (ip, subnet) { print('Carrier: ' + ip + ' : ' + subnet }, 
    function (err) { print('Carrier Err: ' + ip + ' : ' + subnet }
);
var print = function (text) {
    var el = document.getElementById('deviceready');
    var h3 = document.createElement("h3");
    var t = document.createTextNode(text);
    h3.appendChild(t);
    el.appendChild(h3);
}

Finally

cordova run ios     (or android)

P.S. I updated the README to reflect the new feature and also updated the version number to 1.2.0 since a feature was added and semver rules etc.

P.P.S. great plugin. Happy to contribute ;)

Closes #12

salbahra commented 7 years ago

This is wonderful, thank you very much! Thanks for also updating the readme!

dharders commented 7 years ago

No problems! Thanks for the fast turn-around time!