Skip to content

Commit 7c8894b

Browse files
authored
fix(network_info_plus): Improve Wi-Fi IP address retrieval on iOS to avoid null (#3408)
1 parent d421ec0 commit 7c8894b

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

packages/network_info_plus/network_info_plus/example/ios/Runner/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UIKit
22
import Flutter
33

4-
@UIApplicationMain
4+
@main
55
@objc class AppDelegate: FlutterAppDelegate {
66
override func application(
77
_ application: UIApplication,

packages/network_info_plus/network_info_plus/ios/network_info_plus/Sources/network_info_plus/FPPNetworkInfoPlusPlugin.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ - (NSString *)getWifiIP {
6969
__block NSString *addr = nil;
7070
[self enumerateWifiAddresses:AF_INET
7171
usingBlock:^(struct ifaddrs *ifaddr) {
72+
if (addr) return;
7273
addr = [self descriptionForAddress:ifaddr->ifa_addr];
7374
}];
7475
return addr;
@@ -78,6 +79,7 @@ - (NSString *)getWifiIPv6 {
7879
__block NSString *addr = nil;
7980
[self enumerateWifiAddresses:AF_INET6
8081
usingBlock:^(struct ifaddrs *ifaddr) {
82+
if (addr) return;
8183
addr = [self descriptionForAddress:ifaddr->ifa_addr];
8284
}];
8385
return addr;
@@ -87,6 +89,7 @@ - (NSString *)getWifiSubmask {
8789
__block NSString *addr = nil;
8890
[self enumerateWifiAddresses:AF_INET
8991
usingBlock:^(struct ifaddrs *ifaddr) {
92+
if (addr) return;
9093
addr = [self descriptionForAddress:ifaddr->ifa_netmask];
9194
}];
9295
return addr;
@@ -96,6 +99,7 @@ - (NSString *)getWifiBroadcast {
9699
__block NSString *addr = nil;
97100
[self enumerateWifiAddresses:AF_INET
98101
usingBlock:^(struct ifaddrs *ifaddr) {
102+
if (addr) return;
99103
addr = [self descriptionForAddress:ifaddr->ifa_dstaddr];
100104
}];
101105
return addr;
@@ -138,20 +142,22 @@ - (void)enumerateWifiAddresses:(NSInteger)family
138142

139143
// retrieve the current interfaces - returns 0 on success
140144
success = getifaddrs(&interfaces);
141-
if (success == 0) {
142-
// Loop through linked list of interfaces
143-
temp_addr = interfaces;
144-
while (temp_addr != NULL) {
145+
if (success != 0) {
146+
NSLog(@"Error: getifaddrs() failed with error code: %d", success);
147+
return;
148+
}
149+
150+
// Loop through linked list of interfaces
151+
temp_addr = interfaces;
152+
while (temp_addr != NULL) {
145153
if (temp_addr->ifa_addr->sa_family == family) {
146-
// en0 is the wifi connection on iOS
147-
if ([[NSString stringWithUTF8String:temp_addr->ifa_name]
148-
isEqualToString:@"en0"]) {
149-
block(temp_addr);
150-
}
154+
// One of `en` interfaces should be WiFi interface
155+
if (strncmp(temp_addr->ifa_name, "en", 2) == 0) {
156+
block(temp_addr);
157+
}
151158
}
152159

153160
temp_addr = temp_addr->ifa_next;
154-
}
155161
}
156162

157163
// Free memory

0 commit comments

Comments
 (0)