When my iPhone 6(iOS 12.2(16E227)) uses the network that is shared by my iMac, the following method testResolution print: ipAddress: 0.0.0.0
If not use the network shared by iMac, the following method can resolve domain successfully.
- (void)testResolution
{
NSString *hostName = @"www.apple.com";
CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostName);
CFStreamError error;
CFHostStartInfoResolution(host, kCFHostAddresses, &error);
if (0 == error.error)
{
CFArrayRef addressArray = CFHostGetAddressing(host, NULL);
if (addressArray && CFArrayGetCount(addressArray) > 0)
{
NSArray *sockaddrArray = (__bridge NSArray *)addressArray;
for (NSData *sockaddrData in sockaddrArray)
{
struct sockaddr_in *sa_in = (struct sockaddr_in *)[sockaddrData bytes];
const char *ipAddress = inet_ntoa(sa_in->sin_addr);
printf("ipAddress: %s\n", ipAddress);
}
}
}
CFRelease(host);
}