我想执行一些DNS查询,例如获取针对特定域名的IP记录,我在iOS 3.2+ SDK上寻找一种首选方式或一些有用的代码片段.提前
从其他片段开始,我发现了这段代码
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 …Run Code Online (Sandbox Code Playgroud)