3

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?

memmons
  • 40,222
  • 21
  • 149
  • 183
  • Hi There. Did you get this resolved? I'm having the exact same issue where I have the host name string like 'Dave's iPhone' but I would like to convert it to the hostname which in this case would be: "daves-iphone". I could just do character substitution but I'm sure there's a more elegant way! I would really appreciate it if you post your solution. Thanks ! – Enrique R. Jul 21 '10 at 13:28

1 Answers1

1

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)

allenporter
  • 361
  • 1
  • 4
  • I tried this and `hostName` was `nil` inside `netServiceDidPublish`. I believe the hostName property is meant to be used with clients, not servers, in `netServiceDidResolveAddress` – tmm1 Apr 28 '17 at 00:50