1

When the iOS device is connected to a MacBook via a USB cable is there any programatic way for an app running on the iOS device to discover the IP address of the MacBook? This would be so that a socket connection could then be established between the iOS device and a server running on the laptop.

2 Answers 2

2

The IP address on the USB link between them? I'm not even sure there's one in all cases, I believe there's one only when tethering (personal hotspot) is active.

If you mean the IP address of the Mac on Wi-Fi or Ethernet, then there's no guarantee both devices are actually on the same network (you could have the Mac on a local LAN behind a NAT while the iPhone is on a mobile network behind a different NAT), which would make communication problematic (you'd then have the usual P2P NAT-traversal issues, which in turn require external servers).

If you really want the devices to talk to each other over the network (rather than over the USB cable), you should probably look into Bonjour (which requires both devices to be on the same network) or the more recent Multipeer connectivity frameworks (which can even set up a peer-to-peer Wi-Fi network when needed, I believe.

If you really want to communicate over the cable, you're probably better off looking into talking to the device via libimobiledevice / usbmuxd, which can provide communication with a port on the iOS device. Note however that AFAIK, this works with the app on the Mac talking to a server app on the iOS device rather than the opposite.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I'm meaning "the IP address of the Mac on Wi-Fi". Both devices are on the same WiFi network. I guess it sounds like it isn't possible to do it from iPhone->MacBook as you say. I'm looking to communicate over the WiFi connection rather than the USB cable.
Do you need to actually recognise the Mac connected via the USB cable? In that case, have your app listen on a port, and to talk to the app via usbmuxd, to send its address to the app (or a name you could look up via Bonjour). You'll find various ways to do so here. Or simply use Bonjour to discover the server if you don't care which one. Not sure if there's a way to identify the Mac connected to the device by other means.
0

Put this method into your class & you can obtain the IP address of current device.

// get the IP address of current-device - (NSString *)getIPAddress { NSString *address = @"error"; 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 wifi connection on the iPhone if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } freeifaddrs(interfaces); return address; } 

1 Comment

I'm looking for the app on the iOS device to be able to retrieve the IP address of the MacBook. This gets the local IP address. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.