欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS域名解析

来源: 开发者 投稿于  被查看 7884 次 评论:27

iOS域名解析


如何在iOS下进行域名解析

 

//根据域名获取ip地址
-(NSString*)getIPWithHostName:(const NSString*)hostName
{
    const char *hostN= [hostName UTF8String];
    struct hostent* phot;
    
    @try {
        phot = gethostbyname(hostN);
        
    }
    @catch (NSException *exception) {
        return nil;
    }
    
    struct in_addr ip_addr;
    memcpy(&ip_addr, phot->h_addr_list[0], 4);
    char ip[20] = {0};
    inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip));
    
    NSString* strIPAddress = [NSString stringWithUTF8String:ip];
    return strIPAddress;
}

前提需导入头文件

 

 

#include 
#include 
#include 

使用

 

 

    NSString *string = [self getIPWithHostName:@"www.baidu.com"];
    NSLog(@"%@",string);


 

用户评论