NSNetServiceBrowser browse all service type

2.7k views Asked by At

How can I browse for all services regardless of their service type using Bonjour ?

NSNetServiceBrowser can browse for specific servie type - "_music._tcp". Code example:

NSNetServiceBrowser *serviceBrowser;

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

With this code I get all services that have "_music._tcp" service type. For creating service I'm using Network Beacon app.

But I need to find all services regardless of their service type. The answer from this post - doesn't help.

When I'm using:

@"_services._dns-sd._udp."

instead of:

@"_music._tcp."

I get nothing.

I'm beginner iOS programming - I'll be very grateful for the help. Code I used from this book chapter 18.

My code: BonjourViewController.m

@implementation BonjourViewController

- (void)viewDidLoad
{
    [self browseServices];
    [super viewDidLoad];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.services count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];

    if (!cell)
    {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

   //--display the name of each service---
   cell.textLabel.text = [[self.services objectAtIndex:indexPath.row]name];

   return cell;
}

-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
   [self.services addObject:aNetService];
   _debug.text = [_debug.text stringByAppendingString:@"Found service. Resolving address ...\n"];
   [self resolveIPAddress:aNetService];
}

-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:   (NSNetService *)aNetService moreComing:(BOOL)moreComing
{
   [self.services removeObject:aNetService];
   _debug.text = [_debug.text stringByAppendingFormat:@"Removed:  %@\n", [aNetService hostName]];

   [self.tbView reloadData];
}

-(void)resolveIPAddress:(NSNetService *)service
{
   NSNetService *remoteService = service;
   remoteService.delegate = self;
   [remoteService resolveWithTimeout:10];
}

-(void)netServiceDidResolveAddress:(NSNetService *)sender
{
   NSData *address = nil;
   struct sockaddr_in *socketAddres = nil;
   NSString *ipString = nil;
   int port;

   for (int i=0; i<[[sender addresses] count];i++)
   {
      address = [[sender addresses] objectAtIndex: i];
      socketAddress = (struct sockaddr_in *) [address bytes];
      ipString = [NSString stringWithFormat:@"%s", inet_ntoa(socketAddres->sin_addr)];
      port = socketAddres->sin_port;
      _debug.text = [_debug.text stringByAppendingFormat:@"Resolved: %@-->%@:%u\n", [sender hostName], ipString, port];
   }
   [self.tbView reloadData];
}

-(void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
   _debug.text = [_debug.text stringByAppendingFormat:@"Could not resolve:  %@\n", errorDict];
}

-(void)browseServices
{
   self.services = [[NSMutableArray alloc]init];
   self.browser = [[NSNetServiceBrowser alloc]init];
   self.browser.delegate = self;

   [self.browser searchForServicesOfType:@"_music._tcp." inDomain:@""];
} 
0

There are 0 answers