我试图找到一种方法来获取显示在设置-> wi-fi->详细信息-> DNS下的DNS服务器IP。我不知道苹果是否允许以编程方式获取此信息。
我在OS X上使用了它并为我工作(也链接到ibresolv.dylib)
#include <arpa/inet.h> #include <ifaddrs.h> #include <resolv.h> #include <dns.h> - (NSString *) getDNSAddressesStr { NSMutableString *addressStr = [[NSMutableString alloc]initWithString:@"DNS Addresses \n"]; res_state res = malloc(sizeof(struct __res_state)); int result = res_ninit(res); if ( result == 0 ) { for ( int i = 0; i < res->nscount; i++ ) { NSString *s = [NSString stringWithUTF8String : inet_ntoa(res->nsaddr_list[i].sin_addr)]; [addressStr appendFormat:@"%@\n",s]; NSLog(@"%@",s); } } else [addressStr appendString:@" res_init result != 0"]; return addressStr; } }