PUBLISHING A SERVICE

With all the views and outlets wired up, you can publish a service using the NSNetService class. The following Try It Out shows you how.

TRY IT OUT: Publishing a Service on the Network

  1. Using the same project created in the previous section, add the following bold statements to the BonjourAppDelegate.h file:
    #import <UIKit/UIKit.h>
    
    @class BonjourViewController;
    
    @interface BonjourAppDelegate : UIResponder
    <UIApplicationDelegate, NSNetServiceDelegate>
    {
        NSNetService *netService;
    }
    
    @property (strong, nonatomic) UIWindow *window;
    
    @property (strong, nonatomic) BonjourViewController *viewController;
    
    @end
  2. In the BonjourAppDelegate.m file, add the following statements in bold:
    #import“BonjourAppDelegate.h”
    
    #import“BonjourViewController.h”
    
    @implementation BonjourAppDelegate
    
    @synthesize window = _window;
    @synthesize viewController = _viewController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
     (NSDictionary *)launchOptions
    {
        //---publish the service---
        netService = [[NSNetService alloc]
                     initWithDomain:@“”
                     type:@“_MyService._tcp.”
                     name:@“iOS 5 Simulator”
                     port:9876];
        netService.delegate = self;
        [netService publish]; self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[BonjourViewController alloc] initWithNibName: @“BonjourViewController”bundle:nil] autorelease]; self.window.rootViewController = self.viewController; ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.