How to find the IP address of an active AirPlay device?

I am wondering if I can get the IP address of the streaming device that is currently using my iOS app.

Either this, or the IP addresses of all devices that support playback on the network.

Thank you in advance!

EDIT: Although I now have an accepted answer, I am interested to know how to get the IP address of the currently playing device for broadcast.

+6
source share
1 answer

What you need to use is NSNetServiceBrowser for finding devices with protocol. I did the same with printers, my code looks like this:

_netServiceBrowser= [[NSNetServiceBrowser alloc] init]; _netServiceBrowser.delegate= self; [_netServiceBrowser searchForServicesOfType:@"_pdl-datastream._tcp" inDomain:@"local."]; 

You must change @"_pdl-datastream._tcp" for the protocol you want to find, you can find the list of protocols here: http://developer.apple.com/library/mac/#qa/qa1312/_index.html

After that, you need to write the protocol functions:

 #pragma mark - NSNetServiceBrowserDelegate -(void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser{ //prepare the start of the search } -(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing{ //Find a service, remember that after that you have to resolve the service to know the address [_printers addObject:aNetService]; aNetService.delegate=self; [aNetService resolveWithTimeout:5.0]; //More coming says if it has find more services, in case of more service are in queue wait to reload your interface if (!moreComing) { [self.tableView reloadData]; } } -(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict{ //Do what you want in case of error } -(void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser{ //End search! } - (NSString *)getStringFromAddressData:(NSData *)dataIn { //Function to parse address from NSData struct sockaddr_in *socketAddress = nil; NSString *ipString = nil; socketAddress = (struct sockaddr_in *)[dataIn bytes]; ipString = [NSString stringWithFormat: @"%s", inet_ntoa(socketAddress->sin_addr)]; ///problem here return ipString; } - (void)netServiceDidResolveAddress:(NSNetService *)sender { //delegate of NSNetService resolution process [_addresses addObject:[self getStringFromAddressData:[sender.addresses objectAtIndex:0]]]; [self.tableView reloadData]; } 

Website that may be useful: http://www.macresearch.org/cocoa-scientists-part-xxviii-bonjour-and-how-do-you-do

I hope this helps you

+7
source

Source: https://habr.com/ru/post/912907/


All Articles