Skip to content

Commit

Permalink
fix(network_info_plus): Improve Wi-Fi IP address retrieval on iOS to …
Browse files Browse the repository at this point in the history
…avoid null (#3408)
  • Loading branch information
vbuberen authored Dec 20, 2024
1 parent d421ec0 commit 7c8894b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ - (NSString *)getWifiIP {
__block NSString *addr = nil;
[self enumerateWifiAddresses:AF_INET
usingBlock:^(struct ifaddrs *ifaddr) {
if (addr) return;
addr = [self descriptionForAddress:ifaddr->ifa_addr];
}];
return addr;
Expand All @@ -78,6 +79,7 @@ - (NSString *)getWifiIPv6 {
__block NSString *addr = nil;
[self enumerateWifiAddresses:AF_INET6
usingBlock:^(struct ifaddrs *ifaddr) {
if (addr) return;
addr = [self descriptionForAddress:ifaddr->ifa_addr];
}];
return addr;
Expand All @@ -87,6 +89,7 @@ - (NSString *)getWifiSubmask {
__block NSString *addr = nil;
[self enumerateWifiAddresses:AF_INET
usingBlock:^(struct ifaddrs *ifaddr) {
if (addr) return;
addr = [self descriptionForAddress:ifaddr->ifa_netmask];
}];
return addr;
Expand All @@ -96,6 +99,7 @@ - (NSString *)getWifiBroadcast {
__block NSString *addr = nil;
[self enumerateWifiAddresses:AF_INET
usingBlock:^(struct ifaddrs *ifaddr) {
if (addr) return;
addr = [self descriptionForAddress:ifaddr->ifa_dstaddr];
}];
return addr;
Expand Down Expand Up @@ -138,20 +142,22 @@ - (void)enumerateWifiAddresses:(NSInteger)family

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while (temp_addr != NULL) {
if (success != 0) {
NSLog(@"Error: getifaddrs() failed with error code: %d", success);
return;
}

// Loop through linked list of interfaces
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == family) {
// en0 is the wifi connection on iOS
if ([[NSString stringWithUTF8String:temp_addr->ifa_name]
isEqualToString:@"en0"]) {
block(temp_addr);
}
// One of `en` interfaces should be WiFi interface
if (strncmp(temp_addr->ifa_name, "en", 2) == 0) {
block(temp_addr);
}
}

temp_addr = temp_addr->ifa_next;
}
}

// Free memory
Expand Down

0 comments on commit 7c8894b

Please sign in to comment.