我想知道是否有可能获得我的iOS应用目前正在使用的airplay设备的IP地址。
或者网络上所有具有airplay功能的设备的IP地址。
提前谢谢!
编辑:虽然我现在已经接受了答案,但我有兴趣了解如何获取当前正在播放的播放设备的IP。答案 0 :(得分:7)
您必须使用的是NSNetServiceBrowser,以便使用协议搜索设备。我对打印机做了同样的事情,我的代码看起来像:
_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
之后你必须编写协议的功能:
#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-how-do-you-do
我希望它可以帮到你