IPHONE - How to discover network devices with Cocoa Touch?
العربية
български
català
中文
čeština
dansk
Nederlands
eesti
suomi
français
Deutsch
Ελληνικά
עברית
हिंदी
magyar
Bahasa Indonesia
italiano
日本語
한국어
latviešu
lietuvių
norsk
polski
Português
română
русский
slovenčina
slovenski
español
svenska
ไทย
Türkçe
українська
Tiếng Việt
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?
Answer |
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.