iOS如何获得手机ip地址
来源:互联网 发布:relex软件 代理 编辑:程序博客网 时间:2024/05/22 23:59
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];
或者
#import <Foundation/Foundation.h>
@interface NSObject (Getip)
- (NSString *)getIPAddress ;
@end
#import "NSObject+Getip.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
//上面的两个头文件一定要导入
@implementation NSObject (GetIP)
// Get IP Address
- (NSString *)getIPAddress {
NSString *address =@"error";
structifaddrs *interfaces = NULL;
structifaddrs *temp_addr = NULL;
int success =0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success ==0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr !=NULL) {
if(temp_addr->ifa_addr->sa_family ==AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSStringstringWithUTF8String:temp_addr->ifa_name]isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSStringstringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;}
@end
- iOS如何获得手机ip地址
- android 如何获得手机的IP地址代码段
- 获得手机本地IP地址
- 获得手机ip地址(代码)
- 获得iphone手机动态ip地址
- iOS 获取手机ip地址
- iOS 获取手机IP地址
- iOS 获取手机IP地址
- ios获取手机IP地址
- iOS 如何获取手机外网IP地址(附内网IP地址)
- android手机连上电脑热点,如何获得电脑ip地址?
- 如何获得客户端的ip地址
- 如何获得客户端的ip地址
- 如何获得本机IP地址
- 如何获得本机IP地址
- 如何获得客户端的ip地址?
- 如何获得客户端的IP地址
- iOS怎样获取手机的ip地址
- 欢迎使用CSDN-markdown编辑器
- 十、centos设置软件开机自动启动
- Bootstrap popover移动到内容上时不关闭
- 拓扑排序——用C++中STL实现
- lintCode Intersection of Two Arrays II
- iOS如何获得手机ip地址
- RequestDispatcher 的使用总结
- 如何判断一个实例或者一个类实现某接口呢?
- 让UIViewController中的viewDidLoad和viewWillAppear各司其责
- SELECT p.product_name,COUNT(*) FROM `pro_order` p GROUP BY p.product_name HAVING COUNT(*)>10;
- 文章标题C语言实现单链表之基础操作
- 浅析HashMap与ConcurrentHashMap的线程安全性
- 各种移动GPU压缩纹理的使用方法
- springmvc 过滤 不返回值为NULL的数据