Search code examples
iosobjective-ciphoneswiftdns

Get DNS server IP from iphone settings


I try to find a way how to get my DNS server ip which appear under settings->wi-fi->details->DNS. I don't know if apple allow to get this info programmatically.


Solution

  • I used this on OS X and worked for me, (link to ibresolv.dylib as well)

    #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;
        }
    }