13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9: Writing an Application 319{}[_servicesByDomain enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {if ( [key isEqualToString: @"local."] )return; // skip local domain, we've already looked thereNSLog(@"Searching services in domain '%@': %@", key, obj);for ( NSNetService * service in obj ){if ( [[service name] isEqualToString: name] ){NSLog(@"Found service: %@", service);selected = service;*stop = YES;break;}}}];Here you use block-based enumeration to fetch the keys and values from the dictionary in asingle step. When you see the local domain, you skip over it. Should the service remain unfoundeven now, an error is sent back to the caller, as in Listing 9-55.Listing 9-55. Posting Back a Lookup Error// if none were found at all, send back an errorif ( selected == nil ){NSDictionary * info = @{ NSLocalizedDescriptionKey : NSLocalizedString(@"An address bookservice with the provided name could not be found.", @"error") };NSError * error = [NSError errorWithDomain: APRemoteAddressBookErrorDomaincode: APRemoteAddressBookErrorServiceNotFounduserInfo: info];replyHandler(nil, error);return;}Once a service has been found, it needs to be resolved before you can connect to it. You cancheck whether it’s ready to use by sending it a -hostName message; a nil result means it hasn’tyet been resolved.The resolution happens asynchronously by sending -resolveWithTimeout: to the service. Whenthe service resolves or a timeout occurs, a delegate method is sent, using a preconfigured runloop. In this case, use the main run loop since the current thread isn’t going to use one.A block is placed into a dictionary, keyed by the service’s name, to be run when the resolutioncompletes. This block will send back either an error or a proxy for a new APRemoteAddressBookinstance, as seen in Listing 9-56.Listing 9-56. Resolving the Service// resolve the service// take a copy of thewww.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!