iPhone 应用程序中 NSHost 的替代品
我目前正在使用此代码
NSHost *host = [NSHost hostWithAddress:hostname];
if (host == nil) {
host = [NSHost hostWithName:hostname];
if (host == nil) {
[self setMessage:@"Invalid IP address or hostname:"];
return;
}
}
来检索我正在开发的网络应用程序的 IP 地址,但我知道 NSHost 是一个私有 API,将被拒绝。任何人都可以帮助我在不使用 NSHost 的情况下使用此代码来产生相同的结果吗?我不太确定从哪里开始。
编辑:
按照下面看起来近乎完美的建议,我已将此代码添加到我的应用程序中,代替上面的代码,
Boolean result;
CFHostRef hostRef;
CFArrayRef addresses;
NSString *hostname = @"www.apple.com";
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
if (result == TRUE) {
NSLog(@"Resolved");
} else {
NSLog(@"Not resolved");
}
我删除了第四行(因为我已经从其他地方获得了此信息),但我收到了基于周围的错误CFHostRef 未声明。我该如何解决这个问题?这似乎是我唯一的大障碍,因为其他错误只是基于此后无法看到 hostRef 。 编辑:划痕我也得到 kCFHostAddresses 未声明。
I'm currently using this code
NSHost *host = [NSHost hostWithAddress:hostname];
if (host == nil) {
host = [NSHost hostWithName:hostname];
if (host == nil) {
[self setMessage:@"Invalid IP address or hostname:"];
return;
}
}
to retrive my IP Address for a networking app I'm working on, however I'm aware that NSHost is a private API that will be rejected. Can anyone help me with working this code to produce the same results without using NSHost? I'm not really sure where to start.
EDIT:
Following suggestions that seem damn near perfect below I've added this code into my app in the place of the code above
Boolean result;
CFHostRef hostRef;
CFArrayRef addresses;
NSString *hostname = @"www.apple.com";
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
if (result == TRUE) {
NSLog(@"Resolved");
} else {
NSLog(@"Not resolved");
}
I've removed the 4th line (as I have this information from elsewhere already) but I get errors being based around CFHostRef being undeclared. How would I resolve that? It seems to be my only big hurdle, as other errors are only based upon the lack of being able to see hostRef after that.
EDIT: Scratch that I also get kCFHostAddresses undeclared.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CFHost 来实现相同的目的。 CFHost Reference 的顶部是用于进行查找的食谱。
下面的代码执行非常非常基本的同步解析(就像上面使用 NSHost 所做的那样)。请注意,您不想这样做,因为它可能会导致您的应用无响应,因为在解决或超时之前它不会返回。
改用异步查找(CFHostSetClient 和 CFHostScheduleWithRunLoop 如上面的 CFHost 文档中所述)。此外,根据您计划执行的操作,您可能需要考虑使用可达性 API。查看 iPhone 开发者网站上有关网络的 WWDC 会议。
You can use CFHost to achieve the same. On the top of the CFHost Reference is a cookbook recipe for making the lookup.
The following code does very, very basic synchronous resolution (as yours above would with NSHost). Note that you don't want to do this since it can render your app unresponsive because it doesn't return until it's resolved or the timeout hits.
Use asynchronous lookup instead (CFHostSetClient and CFHostScheduleWithRunLoop as described in the CFHost documentation above). Also, depending on what you're planning to do, you may want to look into using the reachability APIs. Check out the WWDC sessions on networking available on the iPhone developer website.
看看这个:http://blog.zachwaugh。 com/post/309927273/programmatically-retriving-ip-address-of-iphone
Look at this: http://blog.zachwaugh.com/post/309927273/programmatically-retrieving-ip-address-of-iphone
http://developer.apple.com/iphone/library/qa/ qa2009/qa1652.html
通过开发者支持系统得到了一个很好的小答案,这非常有效。
http://developer.apple.com/iphone/library/qa/qa2009/qa1652.html
Got a great little answer through the Developer Support system, this worked perfectly.