【发布时间】:2011-02-15 06:10:45
【问题描述】:
我想执行一些 DNS 查询,例如要获取针对特定域名的 IP 记录,我正在为 iOS 3.2+ SDK 寻找首选方式或一些有用的 sn-p。 提前谢谢
我从其他 sn-ps 中找到了这段代码
Boolean result;
CFHostRef hostRef;
NSArray *addresses;
NSString *hostname = @"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 = (NSArray*)CFHostGetAddressing(hostRef, &result);
}
}
if (result == TRUE) {
[addresses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *strDNS = [NSString stringWithUTF8String:inet_ntoa(*((struct in_addr *)obj))];
NSLog(@"Resolved %d->%@", idx, strDNS);
}];
} else {
NSLog(@"Not resolved");
}
但这会为每个主机生成相同的 IP 解析 0->220.120.64.1 有什么帮助吗??
【问题讨论】:
-
当
hostRef为0时,在第二个if语句中使用未初始化的result。