Ionic Cordova plugin Network Interface Plugin 0.0.0.0 IP and Subnet

Ionic Info Below.

Ionic:

   Ionic CLI                     : 5.4.4 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.11.1
   @angular-devkit/build-angular : 0.803.9
   @angular-devkit/schematics    : 8.3.9
   @angular/cli                  : 8.3.9
   @ionic/angular-toolkit        : 2.0.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : ios 5.0.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.0, (and 13 other plugins)

Utility:

   cordova-res : 0.5.1 (update available: 0.8.1)
   native-run  : 0.2.7 (update available: 0.2.9)

System:

   ios-deploy : 1.9.4
   ios-sim    : 8.0.2
   NodeJS     : v10.16.0 (/usr/local/bin/node)
   npm        : 6.12.0
   OS         : macOS Mojave
   Xcode      : Xcode 11.0 Build version 11M337n

Ionic Usage

import { NetworkInterface } from '@ionic-native/network-interface/ngx';

constructor(public networkInterface:NetworkInterface){}

this.networkInterface.getWiFiIPAddress().then(address=>console.log(address));

console output

{"ip":"0.0.0.0","subnet":"0.0.0.0"}

Try to catch some error as the doc suggest:

this.networkInterface.getWiFiIPAddress()
	.then(address => console.info(`IP: ${address.ip}, Subnet: ${address.subnet}`))
	.catch(error => console.error(`Unable to get IP: ${error}`));

Do you get some error with this?

Hi, Thanks for your message. No i don’t get any error. I get an error of course when there is no WiFi connected. Otherwise it just give 0 IP and Subnet.

Are you using a IPv6 network?

No the network is IPv4.

For android I am using Hotspot, HotspotNetwork
plugin and it works good on the same Router/Network.

Try to call the function inside the constructor, as the developer suggests in a GitHub issue.

Also, there is an issue similar to yours, you can keep track there.

He Leanord, Thanks for the message.

I tried that. Still 0 IP. It works with other routers but not with that Huawei ones. Now I cant say there is something wrong with plugin also the router works fine with Android app.

Yes I saw that issue but because of inactivity i posted my own. Thanks for your help

@nomanshah I too was facing the same issue on my iPhone. when i check the native code and printed all the interfaces that it loops looks like this,
[
“lo0 :: 127.0.0.1”,
“lo0 :: 0.0.0.0”,
“lo0 :: 0.0.0.0”,
“en0 :: 0.0.0.0”,
“en0 :: 192.168.18.8”,
“en0 :: 0.0.0.0”,
"en0 :: 0.0.0.0",
“awdl0 :: 0.0.0.0”,
“utun0 :: 0.0.0.0”,
“utun1 :: 0.0.0.0”
]

Plugin is looping all the interfaces of en0 entries and returns the last en0 value to us which in my case it’s 0.0.0.0. i think you’re also facing this same issue.

I just modified the plugin code to stop updating the variable when en0 has some IP which is not equal to 0.0.0.0. and it worked for me.

1 Like

In case anyone is wondering how and where to make these changes to the plugin code:

Changes are to be made in CDVNetworkInterface.m in line 22 (within the getInterfaceiIP function). Search for

if([name isEqualToString:interfaceName])

And change it to this:

if([name isEqualToString:interfaceName] && ![addr isEqualToString:@"0.0.0.0"])

This will make sure that the IP is only used if it’s not 0.0.0.0. I’m sure there are more elegant ways to solve this, but I’m not an expert and it works for me.