Search code examples
iphonecocoabonjour

iPhone SDK: Bonjour & NSNetService name != published name?


In my iPhone app, I'm publishing a bonjour service and using the following delegate method:

- (void)netServiceDidPublish:(NSNetService *)ns
{
   NSLog(@"Bonjour Service Published: http://%@.%@", [ns name], [ns domain]);
}

The "name" property is returning the device name, "How's Testing", which is correct. However, when I use Safari to discover available services the name is "hows-testing" -- the service is http://hows-testing.local.:somePortNumber.

Why is the published name different than what is being reported by the NSNetService? How do I display for the actual name of the published service? Assuming that, for some reason, there is no way to get the published name from the object, how do I determine it myself? I understand that it's based on device name, but what are the substitution rules? Remove apostrophes, replace spaces with dash...anything else? What about special characters?


Solution

  • I believe that "hows-testing" is the host name of the computer, not the name of the service. Instead, you want to look at the hostName attribute:

    - (void)netServiceDidPublish:(NSNetService *)ns
    {
       NSLog(@"Bonjour Service Published: http://%@", [ns hostName]);
    }
    

    You should probably also examine the port attribute, and include it in the URL, if the port is something other than the default for that protocol (80 for HTTP)