我目前使用此代码

    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不宣

有帮助吗?

解决方案 3

http://developer.apple.com/iphone/library /qa/qa2009/qa1652.html

拨通了开发人员支持系统,一个伟大的小的回答,这完美地工作。

其他提示

您可以使用CFHost达到相同的。在 CFHost参考的顶部是用于使查找一个菜谱配方

下面的代码确实很,请非常基本的同步分辨率(如上将与NSHost你的)。需要注意的是你不想这样做,因为它可以使你的应用程序没有响应,因为它不返回,直到它的解决或超时命中。

(适用与上述CFHost文档中描述CFHostSetClient和CFHostScheduleWithRunLoop)使用异步查找代替即可。此外,这取决于你打算做的,你可能要考虑使用的API的可达性。退房的WWDC会议上的iPhone开发者网站上的网络使用。

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top