Skip to content

Commit af75ed0

Browse files
Rapsssitopusherman
authored andcommitted
(feature) updates getIPV4Address to return wifi IP if exists, cellular if not
* [FEATURE] getWIFIIPV4Address * Updated NetworkInfo.js & README * Updated with getWIFIIPV4Address * Fixed conversion to String in Android * Updated for new API * (task) code cleanup
1 parent 9cdf10d commit af75ed0

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

NetworkInfo.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const NetworkInfo = {
2121
},
2222

2323
async getIPV4Address() {
24+
const wifiIP = await RNNetworkInfo.getWIFIIPV4Address();
25+
if (wifiIP && wifiIP !== '0.0.0.0') {
26+
return wifiIP;
27+
}
28+
2429
return await RNNetworkInfo.getIPV4Address();
2530
},
2631

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { NetworkInfo } from "react-native-network-info";
3131
// Get Local IP
3232
const ipAddress = await NetworkInfo.getIPAddress();
3333

34-
// Get IPv4 IP
34+
// Get IPv4 IP (priority: WiFi first, cellular second)
3535
const ipv4Address = await NetworkInfo.getIPV4Address();
3636

3737
// Get Broadcast

android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ public void run() {
161161
}).start();
162162
}
163163

164+
/**
165+
Gets the device's WiFi interface IP address
166+
@return device's WiFi IP if connected to WiFi, else '0.0.0.0'
167+
*/
168+
@ReactMethod
169+
public void getWIFIIPV4Address(final Promise promise) throws Exception {
170+
new Thread(new Runnable() {
171+
public void run() {
172+
try {
173+
WifiInfo info = wifi.getConnectionInfo();
174+
int ipAddress = info.getIpAddress();
175+
String stringip = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
176+
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
177+
promise.resolve(stringip);
178+
}catch (Exception e) {
179+
promise.resolve(null);
180+
}
181+
}
182+
}).start();
183+
}
184+
164185
@ReactMethod
165186
public void getSubnet(final Promise promise) throws Exception {
166187
new Thread(new Runnable() {

ios/RNNetworkInfo.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ @implementation RNNetworkInfo
186186
}
187187
}
188188

189+
/**
190+
Gets the device's WiFi interface IP address
191+
@return device's WiFi IP if connected to WiFi, else '0.0.0.0'
192+
*/
193+
RCT_EXPORT_METHOD(getWIFIIPV4Address:(RCTPromiseResolveBlock)resolve
194+
rejecter:(RCTPromiseRejectBlock)reject)
195+
{
196+
@try{
197+
NSArray *searchArray = @[ IOS_WIFI @"/" IP_ADDR_IPv4 ];
198+
NSDictionary *addresses = [self getAllIPAddresses];
199+
NSLog(@"addresses: %@", addresses);
200+
201+
__block NSString *address;
202+
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
203+
{
204+
address = addresses[key];
205+
if(address) *stop = YES;
206+
} ];
207+
NSString *addressToReturn = address ? address : @"0.0.0.0";
208+
resolve(addressToReturn);
209+
}@catch (NSException *exception) {
210+
resolve(NULL);
211+
}
212+
}
213+
189214
RCT_EXPORT_METHOD(getSubnet:(RCTPromiseResolveBlock)resolve
190215
rejecter:(RCTPromiseRejectBlock)reject)
191216
{

0 commit comments

Comments
 (0)