How to detect network devices using Cocoa Touch?

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

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

this returns results, but when I try to resolve the addresses, I get the following errors:

 NSNetServicesErrorCode = -72004; NSNetServicesErrorDomain = 10; 

I was looking for a mistake and it seems like there is a bad argument?

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

if I search for a specific service, for example [serviceBrowser searchForServicesOfType:@"_ipp._tcp." inDomain:@""]; [serviceBrowser searchForServicesOfType:@"_ipp._tcp." inDomain:@""]; works fine.

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

+7
objective-c iphone cocoa-touch networking
source share
1 answer

This is the right approach . Potentially, the reason you have NSNetServicesBadArgumentError is because your line is serviceType @"_services._dns-sd._udp." incorrect, try @"_services._dns-sd._udp" instead of ie without a finite period.

Apple's documentation is confused in this case. The NSNetServiceBrowser class reference says that:

The serviceType argument must contain information about the type of service and transport level. To ensure that the mDNS responder is looking for services, not hosts, be sure to assign the service name and transport level name to the underscore character ("_"). For example, to search for an HTTP service in TCP, you should use a string like "_http._tcp." Please note that a period character at the end is required.

However, in NSNetServices and CFNetServices Programming Guide, the example for initializing the browser and starting the search does not explicitly use the period at the end:

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

Try this and see if you have any luck.

+11
source share

All Articles