WEBINAR:
On-Demand
Application Security Testing: An Integral Part of DevOps
Bonjour is Apple's implementation of the Zeroconf protocol, which enables the automatic
discovery of computers, devices and services on an IP network. In this article, you will learn
how to implement Bonjour on the iPhone by using the NSNetService class to publish a service.
You will also use the NSNetServiceBrowser class to discover services that have been published.
In the next article, you will learn how to communicate with another device that you have
discovered using TCP/IP.
Creating the Project
Using Xcode, create a View-based Application project and name it as Bonjour.
Double-click on the BonjourViewController.xib file and populate the View window with the
following views (see Figure 1):
* Text Field
* Round Rect Button
* Label
* Table View
* Text View
Figure 1: Populating the View window with the various views.
In the BonjourViewController.h file, add the following statements in bold:
#import <UIKit/UIKit.h>
@interface BonjourViewController : UIViewController {
//---outlets---
IBOutlet UITableView *tbView;
IBOutlet UITextField *message;
IBOutlet UITextView *debug;
}
//---expose the outlets as properties---
@property (nonatomic, retain) UITableView *tbView;
@property (nonatomic, retain) UITextField *message;
@property (nonatomic, retain) UITextView *debug;
//---actions---
-(IBAction) btnConnect:(id)sender;
-(IBAction) btnSend:(id)sender;
-(IBAction) doneEditing;
@end
In the BonjourViewController.xib window, perform the following connections:
* Control-click the File's Owner item and drag and drop it over the Text Field view. Select
message.
* Control-click the File's Owner item and drag and drop it over the Table View. Select
tbView.
* Control-click the File's Owner item and drag and drop it over the Text View. Select
debug.
* Control-click the Send button and drag and drop it over the File's Owner item. Select
btnSend:.
* Control-click the Connect button and drag and drop it over the File's Owner item. Select
btnConnect:.
* Right-click on the Text Field view and connect the Did End on Exit event to the File's Owner
item. Select doneEditing:.
* Right-click on the Table View and connect the dataSource outlet to the File's Owner
item.
* Right-click on the Table View and connect the delegate outlet to the File's Owner item.
To verify that all the connections are made correctly, right-click on the File's Owner item
and view its connections (see Figure 2).
Figure 2: Verifying that all the connections are made correctly.
Publishing a Service
With all the views and actions wired up, let's start by seeing how you can publish a service.
In the BonjourAppDelegate.h file, add the following statements in bold:
#import <UIKit/UIKit.h>
@class BonjourViewController;
@interface BonjourAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
BonjourViewController *viewController;
//---use this to publish a service---
NSNetService *netService;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet
BonjourViewController *viewController;
@end
Basically, you will use the NSNetService class to publish your presence on the network.
In the BonjourAppDelegate.m file, add the following statements in bold:
#import "BonjourAppDelegate.h"
#import "BonjourViewController.h"
@implementation BonjourAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
//---publish the service---
netService = [[NSNetService alloc]
; initWithDomain:@""
; type:@"_My
Service._tcp."
; name:@""
; port:9876]
;
netService.delegate = self;
[netService publish];
}
-(void)netService:(NSNetService *)aNetService
didNotPublish:(NSDictionary *)dict {
NSLog(@"Service did not publish: %@", dict);
}
- (void)applicationWillTerminate:(UIApplication *)application {
//---stop the service when the application is terminated---
[netService stop];
}
- (void)dealloc {
[netService release];
[viewController release];
[window release];
[super dealloc];
}
@end
Here, you advertise your presence on the network by publishing a network service when your
application has finished launching (applicationDidFinishLaunching:). You publish a network
service by passing several parameters to the NSNetService class:
netService = [[NSNetService alloc]
; initWithDomain:@""
; type:@"_My
Service._tcp."
; name:@""
; port:9876]
;
The first argument specifies the domain for the service. You use @"" to denote the default
domain. The second argument indicates the service type and transport layer. In this example, I
named the service as MyService and it uses TCP. Note that you need to prefix the service name
and protocol with a "_" and end the protocol with a ".". The third argument specifies the name
of the service, which must be a unique name (I have used an empty string in this case).
Finally, specify the port number on which the service is published via the fourth argument.
You also implemented the netService:didNotPublish: method so that in the event the service is
not published successfully, you will write a message to the debugger console.
When the application exits (applicationWillTerminate:) you will stop publishing the service.
Browsing for Services
Now that you have seen how to publish a service, let's see how you can browse for services that
have been published on the network.