gpt4 book ai didi

objective-c - 如何找到事件 AirPlay 设备的 IP 地址?

转载 作者:可可西里 更新时间:2023-11-01 03:50:00 24 4
gpt4 key购买 nike

我想知道是否可以获取我的 iOS 应用当前使用的 airplay 设备的 IP 地址。

要么是那个,要么是网络上所有支持播放的设备的 IP 地址。

提前致谢!

编辑:虽然我现在有了一个可以接受的答案,但我很想知道如何获取当前播放的 airplay 设备的 IP。

最佳答案

您必须使用 NSNetServiceBrowser 来搜索具有该协议(protocol)的设备。我对打印机做了同样的事情,我的代码如下:

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

您必须为要搜索的协议(protocol)更改@"_pdl-datastream._tcp",您可以在此处找到协议(protocol)列表:http://developer.apple.com/library/mac/#qa/qa1312/_index.html

之后你必须编写协议(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-how-do-you-do

希望对你有帮助

关于objective-c - 如何找到事件 AirPlay 设备的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10101935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com