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

喜夏-厌秋 提交于 2020-01-02 04:24:16

问题


I am wondering if it´s possible to get the IP address of the airplay device my iOS app is currently using.

Either that, or the IP-addresses of all airplay capable devices on the network.

Thank you in advance!

EDIT: Although I now have an accepted answer, I´m interested in knowing how to get the IP of the currently playing airplay device.


回答1:


What you have to use is NSNetServiceBrowser to do a search of the devices with the protocol. I have did the same with printers, my code looks like:

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

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

After that you have to write the functions of the protocol:

#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];
}

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

I hope it helps you



来源:https://stackoverflow.com/questions/10101935/how-to-find-the-ip-address-of-the-active-airplay-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!