BROWSING FOR SERVICES

Now that you have seen how to publish a service, this section demonstrates how you can browse for services that have been published on the network. You will use the NSNetServiceBrowser class to discover services published on the network.

TRY IT OUT: Browsing for Services on the Network

  1. Using the Bonjour project from the previous Try it Out, add the following bold statements to the BonjourViewController.h importfile:
    #import <arpa/inet.h>
    
    @interface BonjourViewController : UIViewController
    <UITableViewDelegate, UITableViewDataSource,
     NSNetServiceDelegate, NSNetServiceBrowserDelegate>
    {
        IBOutlet UITableView *tbView;
        IBOutlet UITextView *debug;
        NSNetServiceBrowser *browser;
        NSMutableArray *services;
    }
    
    @property (nonatomic, retain) UITableView *tbView;
    @property (nonatomic, retain) UITextView *debug;
    @property (nonatomic, retain) NSNetServiceBrowser *browser;
    @property (nonatomic, retain) NSMutableArray *services;
    
    -(void) resolveIPAddress:(NSNetService *)service;
    -(void) browseServices;
    
    @end
  2. In the BonjourViewController.m file, add the following bold statements:
    #import“BonjourViewController.h”
    
    @implementation BonjourViewController
    
    @synthesize tbView;
    @synthesize debug;
    
    @synthesize browser;
    @synthesize services;
    
    -(NSInteger) tableView:(UITableView *)tableView
      numberOfRowsInSection:(NSInteger)section {
        return [self.services count];
    }
    
    -(UITableViewCell *) tableView:(UITableView *)tableView  cellForRowAtIndexPath:
             (NSIndexPath *)indexPath {
        static NSString *CellIdentifier ...

Get Beginning iOS 5 Application Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.