iPhone 应用程序中 NSHost 的替代品

发布于 2024-09-13 21:49:59 字数 1117 浏览 5 评论 0原文

我目前正在使用此代码

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有你我更好 2024-09-20 21:49:59

您可以使用 CFHost 来实现相同的目的。 CFHost Reference 的顶部是用于进行查找的食谱。

下面的代码执行非常非常基本的同步解析(就像上面使用 NSHost 所做的那样)。请注意,您不想这样做,因为它可能会导致您的应用无响应,因为在解决或超时之前它不会返回。

改用异步查找(CFHostSetClient 和 CFHostScheduleWithRunLoop 如上面的 CFHost 文档中所述)。此外,根据您计划执行的操作,您可能需要考虑使用可达性 API。查看 iPhone 开发者网站上有关网络的 WWDC 会议。

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");
}

// Don't forget to release hostRef when you're done with it

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.

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");
}

// Don't forget to release hostRef when you're done with it
﹎☆浅夏丿初晴 2024-09-20 21:49:59

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文