小编yas*_*urk的帖子

如何在iOS上执行DNS查询

我想执行一些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)

iphone ipad ios

15
推荐指数
2
解决办法
2万
查看次数

标签 统计

ios ×1

ipad ×1

iphone ×1