iOS怎样获取手机的ip地址

时间:2021-03-19 13:40:02

      第一次写博客,以后大家多多支持:

经常在电视剧里面看黑客盗取密码。。。。。好崇拜,我也在不断的学习,学习ios一段时间了,我在想:“怎样获取苹果手机的ip地址呢?”,于是我就百度,经过查找资料,终于找到了一种方法:

1.添加这五个库(在联网的情况下)

#import <sys/socket.h>

#import <sys/sockio.h>

#import <sys/ioctl.h>

#import <net/if.h>

#import <arpa/inet.h>


2.写一个方法

- (NSString *)getDeviceIPIpAddresses


{

    

    int sockfd =socket(AF_INET,SOCK_DGRAM,0);

    

//    if (sockfd <</span> 0) return nil;

    

    NSMutableArray *ips = [NSMutableArrayarray];

    

    

    

    int BUFFERSIZE =4096;

    

    structifconf ifc;

    

    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;

    

    structifreq *ifr, ifrcopy;

    

    ifc.ifc_len = BUFFERSIZE;

    

    ifc.ifc_buf = buffer;

    

    if (ioctl(sockfd,SIOCGIFCONF, &ifc) >=0){

        

        for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){

            

            ifr = (structifreq *)ptr;

            

            int len =sizeof(structsockaddr);

            

            if (ifr->ifr_addr.sa_len > len) {

                

                len = ifr->ifr_addr.sa_len;

                

            }

            

            ptr += sizeof(ifr->ifr_name) + len;

            

            if (ifr->ifr_addr.sa_family !=AF_INET)continue;

            

            if ((cptr = (char *)strchr(ifr->ifr_name,':')) != NULL) *cptr =0;

            

            if (strncmp(lastname, ifr->ifr_name,IFNAMSIZ) == 0)continue;

            

            memcpy(lastname, ifr->ifr_name,IFNAMSIZ);

            

            ifrcopy = *ifr;

            

            ioctl(sockfd,SIOCGIFFLAGS, &ifrcopy);

            

            if ((ifrcopy.ifr_flags &IFF_UP) ==0)continue;

            

            

            

            NSString *ip = [NSString stringWithFormat:@"%s",inet_ntoa(((structsockaddr_in *)&ifr->ifr_addr)->sin_addr)];

            

            [ips addObject:ip];

            

        }

        

    }

    

    close(sockfd);

    

    

    

    

    

    NSString *deviceIP =@"";

    

    for (int i=0; i < ips.count; i++)

        

    {

        

        if (ips.count >0)

            

        {

            

            deviceIP = [NSStringstringWithFormat:@"%@",ips.lastObject];

            

            

            

        }

        

    }

    

    NSLog(@"deviceIP========%@",deviceIP);

    return deviceIP;

    

}



3.调用本方法即可

 [selfgetDeviceIPIpAddresses];


就这么简单---------问题解决了

iOS怎样获取手机的ip地址iOS怎样获取手机的ip地址