gpt4 book ai didi

iphone - iPhone 应用程序中 NSHost 的替代品

转载 作者:行者123 更新时间:2023-12-03 19:00:02 25 4
gpt4 key购买 nike

我目前正在使用此代码

    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 是私有(private) 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。

最佳答案

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

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

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

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

关于iphone - iPhone 应用程序中 NSHost 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434192/

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