Sowjanya lankisetty
Sowjanya lankisetty

Reputation: 210

To my MAC both Ethernet and wifi are connected.How can i know from where i am acessing internet

When i am connected to both Ehernet and wifi. I will get two ip addresses assigned to interfaces say en0 and en1. By using shell command i am achieving this by getting current route interface route get default | grep interface | awk '{print $2}' and then comparing with network service order networksetup -listnetworkserviceorder

Is there any framework in MAC, which helpa us to know current interface used.So that i can programatically do it in objective-c

Upvotes: 1

Views: 86

Answers (1)

AppleDeveloper
AppleDeveloper

Reputation: 1443

We can get current interface using below code

- (NSString*)defaultRouter {

    SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myApplication"), NULL, NULL);
    CFDictionaryRef dr6 = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv6"));
    CFDictionaryRef dr4 = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4"));
    if(dr6)
      {
        CFStringRef router = CFDictionaryGetValue(dr6, CFSTR("PrimaryInterface"));
        NSString *routerString = [NSString stringWithString:(__bridge NSString *)router];
        self.primaryInterface=routerString;
           return self.primaryInterface;
                CFRelease(dr6);

      }
    else if(dr4)
      {

        CFStringRef router = CFDictionaryGetValue(dr4, CFSTR("PrimaryInterface"));
        NSString *routerString = [NSString stringWithString:(__bridge NSString *)router];
        NSLog(@"%@", routerString);
        self.primaryInterface=routerString;
           return self.primaryInterface;
                CFRelease(dr4);
      }
    CFRelease(ds);
    return 0;

  }

After getting the primary Interface use this to get adapter typr(Wi-Fi or Ethernet)

SCNetworkInterfaceGetLocalizedDisplayName(primaryInterface) 

Upvotes: 1

Related Questions