如何查找活动AirPlay设备的IP地址? [英] How to find the IP-address of the active AirPlay device?
问题描述
我想知道是否有可能获得我的iOS应用目前正在使用的airplay设备的IP地址。
I am wondering if it´s possible to get the IP address of the airplay device my iOS app is currently using.
要么是这个,要么是IP地址网络上所有具有airplay功能的设备。
Either that, or the IP-addresses of all airplay capable devices on the network.
提前感谢您!
编辑:虽然我现在有一个接受的答案,我有兴趣知道如何获得当前正在播放的播放设备的IP。
Although I now have an accepted answer, I´m interested in knowing how to get the IP of the currently playing airplay device.
推荐答案
你有什么使用NSNetServiceBrowser来搜索具有该协议的设备。我对打印机做了同样的事情,我的代码如下:
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."];
您必须更改 @_ pdl-datastream._tcp
对于您要搜索的协议,您可以在此处找到协议列表: http://developer.apple.com/library/mac/#qa/qa1312/_index.html
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];
}
一个有用的网站:
http://www.macresearch.org/cocoa-scientists- part-xxviii-bonjour-and-do-do-do-do
我希望它可以帮到你
这篇关于如何查找活动AirPlay设备的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!