Skip to content

Commit

Permalink
feat(iOS): Detect Apple TV wired connection and retrieve the Wifi IP …
Browse files Browse the repository at this point in the history
…address & subnet (#229 by @gcesarmza)
  • Loading branch information
Gustavo Genovese authored and matt-oakes committed Oct 6, 2019
1 parent bda8e03 commit 2d2d167
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions ios/RNCConnectionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static NSString *const RNCConnectionTypeUnknown = @"unknown";
static NSString *const RNCConnectionTypeNone = @"none";
static NSString *const RNCConnectionTypeWifi = @"wifi";
static NSString *const RNCConnectionTypeCellular = @"cellular";
static NSString *const RNCConnectionTypeEthernet = @"ethernet";

// Based on the EffectiveConnectionType enum described in the W3C Network Information API spec
// (https://wicg.github.io/netinfo/).
Expand Down
27 changes: 27 additions & 0 deletions ios/RNCConnectionState.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#endif

#if TARGET_OS_TV
#include <ifaddrs.h>
#endif

@implementation RNCConnectionState

// Creates a new "blank" state
Expand Down Expand Up @@ -64,6 +68,29 @@ - (instancetype)initWithReachabilityFlags:(SCNetworkReachabilityFlags)flags
#endif
else {
_type = RNCConnectionTypeWifi;
#if TARGET_OS_TV
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// 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 (temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the ethernet connection on the Apple TV
NSString* ifname = [NSString stringWithUTF8String:temp_addr->ifa_name];
if ([ifname isEqualToString:@"en0"]) {
_type = RNCConnectionTypeEthernet;
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
#endif
}
}
return self;
Expand Down
20 changes: 16 additions & 4 deletions ios/RNCNetInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,14 @@ - (NSString *)ipAddress
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
NSString* ifname = [NSString stringWithUTF8String:temp_addr->ifa_name];
if (
// Check if interface is en0 which is the wifi connection on the iPhone
// and the ethernet connection on the Apple TV
[ifname isEqualToString:@"en0"] ||
// Check if interface is en1 which is the wifi connection on the Apple TV
[ifname isEqualToString:@"en1"]
) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
Expand All @@ -172,8 +178,14 @@ - (NSString *)subnet
temp_addr = interfaces;
while (temp_addr != NULL) {
if (temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
NSString* ifname = [NSString stringWithUTF8String:temp_addr->ifa_name];
if (
// Check if interface is en0 which is the wifi connection on the iPhone
// and the ethernet connection on the Apple TV
[ifname isEqualToString:@"en0"] ||
// Check if interface is en1 which is the wifi connection on the Apple TV
[ifname isEqualToString:@"en1"]
) {
// Get NSString from C String
subnet = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}
Expand Down

0 comments on commit 2d2d167

Please sign in to comment.