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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9: Writing an Application 287Following this is the list of client connection handlers, which is literally just a set used to keepa reference to those objects. Below that is a similar set, this time keeping track of the addressbook objects being vended to your main app and the previously discussed NSMapTable of serviceresolution handlers.The initializer is fairly straightforward, creating the collection objects ahead of time, andinitializing the NSNetServiceBrowser and telling it to search for services in the default domains ofyour type: _apad._tcp.Since this is ARC, you wouldn’t normally need to implement -dealloc, but when using theNSNetService API it’s very important to stop any and all services you’ve resolved, as well asstopping any browsers. If you don’t do this, then system resources will remain tied up dealingwith these services, even though the application that requested them is no longer running.Service DiscoveryThe initializer method fired off the browser by calling NSNetServiceBrowser’s -searchForServicesOfType:inDomain: method. The results are returned through a pair of delegate methodsimplemented by this object. In these the browser returns any discovered or removed servicesone by one. Each time a service is delivered, the browser also provides information on whethermore services remain to be delivered or whether this is the last service that has been discoveredpresently. This allows delegates to, for example, accumulate the entire list of services beforeprocessing.In this application, you simply keep track of these services in arrays keyed to their domains,adding them in one method and removing them in the other, shown in Listing 9-19.Listing 9-19. Handling Service Discovery- (void) netServiceBrowser: (NSNetServiceBrowser *) aNetServiceBrowserdidFindService: (NSNetService *) aNetServicemoreComing: (BOOL) moreComing{NSMutableArray * servicesInDomain = _servicesByDomain[aNetService.domain];if ( servicesInDomain == nil ){servicesInDomain = [NSMutableArray new];_servicesByDomain[aNetService.domain] = servicesInDomain;}}[servicesInDomain addObject: aNetService];- (void) netServiceBrowser: (NSNetServiceBrowser *) aNetServiceBrowserdidRemoveService: (NSNetService *) aNetServicemoreComing: (BOOL) moreComing{[aNetService stop];NSMutableArray * servicesInDomain = _servicesByDomain[aNetService.domain];[servicesInDomain removeObject: aNetService];}www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!