Errata for Chapter 29


#1

In this chapter, you build on the Bonjour code. However, there is a subtle change you have to make that we made a mistake on:

In NotifiedAppDelegate.m, match your implementation of the following three methods to what is here:

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreServicesComing
{
    if (!desktopServer && [[aNetService name] isEqualToString:@"CocoaHTTPServer"]) {
        [aNetService retain];
        [aNetService resolveWithTimeout:30];
        [aNetService setDelegate:self];
        [statusLabel setText:@"Resolveing CocoaHTTPServer..."];
    } else {
        NSLog(@"ignoring %@", aNetService);
    }
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
    [statusLabel setText:@"Resolved service..."];
    desktopServer = sender;
    [self postInformationToServer];
}

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
    [statusLabel setText:@"Could not resolve service."];
    NSLog(@"%@", errorDict);
    [sender release];
}

#2

Is it just me, or does the code (non-bold) for NotifiedAppDelegate.m look nothing whatsoever like the code I have from chapter 28? I have (comments removed):

- (void) postInformationToServer
{
    [statusLabel setText:@"Sending data to server..."];
    NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                       [[UIDevice currentDevice] name], @"name", nil];
    
    NSData *data = [NSPropertyListSerialization
                    dataWithPropertyList:d format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
    
    NSString *urlString = [NSString stringWithFormat:@"http://%@/register", [self serverHostName]];
    
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
                                
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:data];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    
    [connection start];
}

As far as I can tell, the last time we touched postInformationToServer: was on page 476.

It looks like the code after the [statusLabel setText:…] statement up to the UInt16 port = … line should also be bold. But I’m not sure if another block of code that should have been inserted before was also omitted, or what.

ETA: the line:

NSString *urlString = [NSString stringWithFormat:@"http://%@/register", [self serverHostName]];

has also been changed, to:

NSString *urlString = [NSString stringWithFormat:@"http://%s:%u/register", ipAddress, port];

#3

babasyzygy is correct. That code in postInformationToServer looks quite different than the code from Chapter 28. You’ll have to ignore the bold markings and redo much of that method as per the code snippets on pg 486 and 487.

Also note that the following line is not correct:

it should be:


#4

Joe,

I have a question on a section of the fix you posted:

if (!desktopServer && [[aNetService name] isEqualToString:@"CocoaHTTPServer"]) {
        [aNetService retain];
        [aNetService resolveWithTimeout:30];
        [aNetService setDelegate:self];
        [statusLabel setText:@"Resolveing CocoaHTTPServer..."];
    } else {
        NSLog(@"ignoring %@", aNetService);
    }

I’m guessing you used [aNetService retain] to avoid a false-positive when checking for the existence of desktopServer in the method postInformationToServer. My question is if the project is using ARC, does the compiler ignore all retain messages?