Skip to content

fix(network_info_plus): Improve Wi-Fi IP address retrieval on iOS to avoid null #3408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading