How to perform DNS query on iOS

后端 未结 2 709
暗喜
暗喜 2020-12-01 01:36

i want to perform some DNS queries e.g. to get IP records against a specific domain name, i am looking for a preferred way or some useful snippet for this on iOS 3.2+ SDK. t

2条回答
  •  一整个雨季
    2020-12-01 02:26

    Bros there is a lot simpler way! Thanks to iOS being a unix system, you become a god with unlimited power and resource! I present elegance.

    - (NSString*)lookupHostIPAddressForURL:(NSURL*)url
    {
        // Ask the unix subsytem to query the DNS
        struct hostent *remoteHostEnt = gethostbyname([[url host] UTF8String]);
        // Get address info from host entry
        struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
        // Convert numeric addr to ASCII string
        char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
        // hostIP
        NSString* hostIP = [NSString stringWithUTF8String:sRemoteInAddr];
        return hostIP;
    }
    

提交回复
热议问题