Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discover network devices with Cocoa Touch?

I want to be able to enumerate the names of devices on a local network from a device running iPhone OS 3.x (iPhone/iPad). I have tried using NSNetServiceBrowser to find all services like so:

[serviceBrowser searchForServicesOfType:@"_services._dns-sd._udp." inDomain:@"local."];

this returns results but when I try and resolve the addresses I get the following errors back

NSNetServicesErrorCode = -72004;
NSNetServicesErrorDomain = 10;

I have looked up the error and it appears there is a bad argument?

[kCFNetServiceErrorBadArgument
A required argument was not provided or was not valid.]

if I do a service specific search like [serviceBrowser searchForServicesOfType:@"_ipp._tcp." inDomain:@""]; resolutions works fine.

So, am I on the right track with NSNetServiceBrowser or is there some other method that will allow me to enumerate the names of devices connected to my network?

like image 450
withakay Avatar asked Jun 19 '10 16:06

withakay


People also ask

How Apple Find My network works?

If you open up the Find My app and navigate to the Items tab, you can see all of your Find My-compatible accessories on a map. You'll see current location if the accessory is able to communicate with your ‌iPhone‌, ‌iPad‌, or Mac over Bluetooth, or the last known location if it is too far away.

Why do apps want to find and connect to devices on my network?

“Apps that access your local network can collect information about nearby devices in order to determine which networks you join and when. This information could be used to create a profile of you.


1 Answers

This is the correct approach. Potentially, the reason you have a NSNetServicesBadArgumentError is because your serviceType string @"_services._dns-sd._udp." is invalid try @"_services._dns-sd._udp" instead i.e. without the trailing period.

Apple's documentation is confusing on this point. In the NSNetServiceBrowser Class Reference it states that:

The serviceType argument must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, rather than hosts, make sure to prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string “_http._tcp.“. Note that the period character at the end is required.

However, in the NSNetServices and CFNetServices Programming Guide, the example for Initializing the Browser and Starting a Search clearly doesn't use a period at the end:

serviceBrowser = [[NSNetServiceBrowser alloc] init];
[serviceBrowser setDelegate:delegateObject];
[serviceBrowser searchForServicesOfType:@"_music._tcp" inDomain:@""];

Try it without & see if ou have any luck.

like image 69
tdbit Avatar answered Sep 21 '22 00:09

tdbit