iOS 获取手提式有线电话机ip 地址

By admin in Python on 2019年9月11日

有的时候大家会遇上一种必要,去手提式有线话机用户的IP
地址,判别借使是这种地址,将要做一些遮挡什么的操作,那么大家来看一下怎么手提式有线电话机ip
地址搜罗 ip 推断第有时间你会想到
,去找一下有哪些系统的方法,作者也找了非常久,家徒四壁,最后发生了贰个新的笔触

亟待在联网的景观下才干收获到

图片 1AB7540C2-10B0-4671-A9D2-DABE8A618CD4.png

率先要求导入头文件

#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <net/if.h>
#import <arpa/inet.h>

基于上海体育场面的笔触,访谈二个询问ip
的网页,通过正则表明式,把ip截取下来,然后在按必要实行想要的操作就可以上面来看下代码段

先是种方法 亲测有用

- (NSString *)getDeviceIPIpAddresses{   
 int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
   if (sockfd <<sockfd>>0){   
     returning;   
 } 
   NSMutableArray *ips = [NSMutableArrayarray];   
 int BUFFERSIZE = 4096; 
   strut ifconf ifc;   
 char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ],*cptr;
    struct ifreq *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];        }   
 } 
   return deviceIP;
}
+checkIp{ CheckReach *r =[CheckReach reachabilityForInternetConnection]; if([r currentReachabilityStatus]== NotReachable) return ; //ip1 dispatch_async(dispatch_get_global_queue, ^{ // something //匹配ip地址的正则表达式 NSString *regexStr = @"\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b"; NSString *ip1 = [IpChecker getIp:@"http://www.ip38.com/" withRegexString:regexStr]; NSLog(@"当前的ip 地址是 %@",ip1); });}+(NSString *)getIp :(NSString *)urlStr withRegexString :(NSString *)regexStr{ // 如果网址中存在中文,进行URLEncode NSString *newUrlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 2.构建网络URL对象, NSURL NSURL *url = [NSURL URLWithString:newUrlStr]; if (url == nil) { return @""; } // 3.创建网络请求 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; // 创建同步链接 NSURLResponse *response = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(data==nil) return @""; //返回静态网页字符串 NSString *responseStr = STRING_SAFELY([[NSString alloc] initWithData:data encoding:0x80000632]); NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error]; NSString *result = @""; if (regex != nil) { NSTextCheckingResult *firstMatch=[regex firstMatchInString:responseStr options:0 range:NSMakeRange(0, [responseStr length])]; if (firstMatch) { NSRange resultRange = [firstMatch rangeAtIndex:0]; //从urlString当中截取数据 result= STRING_SAFELY([responseStr substringWithRange:resultRange]); //输出结果 } } return result;}

上边包车型客车是github
地址,有如何难题,能够加微信或qq钻探也许研商,https://github.com/heysunnyboy/ipChecker.git

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 韦德国际手机网站 版权所有