我想執行一些DNS查詢,例如爲了獲得針對特定域名的IP記錄,我正在尋找iOS 3.2+ SDK上的首選方式或一些有用的片段。 thanx提前如何在iOS上執行DNS查詢
部分來自其他片段,我發現這個代碼
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");
}
但解決0-> 220.120.64.1任何幫助,爲每個主機,這是生產相同的IP?
當`hostRef`爲'0'時,在第二個`if`語句中使用未初始化的`result`。 – 2015-05-28 09:18:50