iOS: 如何获取ios设备的当前IP地址

时间:2022-08-27 14:38:07

有的时候,我们项目上线后,需要根据ip地址去统计不同地区的用户情况,此时IP地址的收取显得尤其重要,一般情况下,在用户登录时去获取用户的ip是准确的,当然实时追踪ip的变化而统计是更安全可靠的。

ip地址长度现在是有区别的,分为IPv4和IPv6.IPv4地址是类似 A.B.C.D 的格式,它是32位,用\".\"分成四段,用10进制表示;而IPv6地址类似X:X:X:X:X:X:X:X的格式,它是128位的,用\":\"分成8段,用16进制表示;可见,IPv6地址空间相对于IPv4地址有了极大的扩充。

IPv4是32位地址长度
IPv6是128位地址长度

 下面有两个方法,可供使用,如下:

方法一:单独创建一个c文件,写一套方法去获取

IPAddress.h:声明文件

iOS: 如何获取ios设备的当前IP地址
//
// IPAddress.h
// IP_Test
//
// Created by mac on 16/7/15.
// Copyright © 2016年 xiayuanquan. All rights reserved.
//


#ifndef IPAddress_h
#define IPAddress_h

#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<unistd.h>
#include
<sys/ioctl.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<netdb.h>
#include
<arpa/inet.h>
#include
<sys/sockio.h>
#include
<net/if.h>
#include
<errno.h>
#include
<net/if_dl.h>
#include
<net/ethernet.h>

#define BUFFERSIZE 4000
#define MAXADDRS 32
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

extern char *if_names[MAXADDRS];
extern char *ip_names[MAXADDRS];
extern char *hw_addrs[MAXADDRS];
extern unsigned long ip_addrs[MAXADDRS];

// Function prototypes
void InitAddresses();
void FreeAddresses();
void GetIPAddresses();
void GetHWAddresses();


#endif /* IPAddress_h */
iOS: 如何获取ios设备的当前IP地址

IPAddress.c:实现文件

iOS: 如何获取ios设备的当前IP地址
//
// IPAddress.c
// IP_Test
//
// Created by mac on 16/7/15.
// Copyright © 2016年 xiayuanquan. All rights reserved.
//

#include
"IPAddress.h"

char *if_names[MAXADDRS];
char *ip_names[MAXADDRS];
char *hw_addrs[MAXADDRS];
unsigned
long ip_addrs[MAXADDRS];

static int nextAddr = 0;

void InitAddresses()
{
int i;
for (i=0; i<MAXADDRS; ++i)
{
if_names[i]
= ip_names[i] = hw_addrs[i] = NULL;
ip_addrs[i]
= 0;
}
}

void FreeAddresses()
{
int i;
for (i=0; i<MAXADDRS; ++i)
{
if (if_names[i] != 0) free(if_names[i]);
if (ip_names[i] != 0) free(ip_names[i]);
if (hw_addrs[i] != 0) free(hw_addrs[i]);
ip_addrs[i]
= 0;
}
InitAddresses();
}

void GetIPAddresses()
{
int i, len, flags;
char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
struct ifconf ifc;
struct ifreq *ifr, ifrcopy;
struct sockaddr_in *sin;

char temp[80];

int sockfd;

for (i=0; i<MAXADDRS; ++i)
{
if_names[i]
= ip_names[i] = NULL;
ip_addrs[i]
= 0;
}

sockfd
= socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror(
"socket failed");
return;
}

ifc.ifc_len
= BUFFERSIZE;
ifc.ifc_buf
= buffer;

if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
{
perror(
"ioctl error");
return;
}

lastname[
0] = 0;

for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
{
ifr
= (struct ifreq *)ptr;
len
= max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
ptr
+= sizeof(ifr->ifr_name) + len; // for next one in buffer

if (ifr->ifr_addr.sa_family != AF_INET)
{
continue; // ignore if not desired address family
}

if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
{
*cptr = 0; // replace colon will null
}

if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
{
continue; /* already processed this interface */
}

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

ifrcopy
= *ifr;
ioctl(sockfd, SIOCGIFFLAGS,
&ifrcopy);
flags
= ifrcopy.ifr_flags;
if ((flags & IFF_UP) == 0)
{
continue; // ignore if interface not up
}

if_names[nextAddr]
= (char *)malloc(strlen(ifr->ifr_name)+1);
if (if_names[nextAddr] == NULL)
{
return;
}
strcpy(if_names[nextAddr], ifr
->ifr_name);

sin
= (struct sockaddr_in *)&ifr->ifr_addr;
strcpy(temp, inet_ntoa(sin
->sin_addr));

ip_names[nextAddr]
= (char *)malloc(strlen(temp)+1);
if (ip_names[nextAddr] == NULL)
{
return;
}
strcpy(ip_names[nextAddr], temp);

ip_addrs[nextAddr]
= sin->sin_addr.s_addr;

++nextAddr;
}

close(sockfd);
}

void GetHWAddresses()
{
struct ifconf ifc;
struct ifreq *ifr;
int i, sockfd;
char buffer[BUFFERSIZE], *cp, *cplim;
char temp[80];

for (i=0; i<MAXADDRS; ++i)
{
hw_addrs[i]
= NULL;
}

sockfd
= socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror(
"socket failed");
return;
}

ifc.ifc_len
= BUFFERSIZE;
ifc.ifc_buf
= buffer;

if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror(
"ioctl error");
close(sockfd);
return;
}

ifr
= ifc.ifc_req;

cplim
= buffer + ifc.ifc_len;

for (cp=buffer; cp < cplim; )
{
ifr
= (struct ifreq *)cp;
if (ifr->ifr_addr.sa_family == AF_LINK)
{
struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
int a,b,c,d,e,f;
int i;

strcpy(temp, (
char *)ether_ntoa((const struct ether_addr *)LLADDR(sdl)));
sscanf(temp,
"%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
sprintf(temp,
"%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);

for (i=0; i<MAXADDRS; ++i)
{
if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
{
if (hw_addrs[i] == NULL)
{
hw_addrs[i]
= (char *)malloc(strlen(temp)+1);
strcpy(hw_addrs[i], temp);
break;
}
}
}
}
cp
+= sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
}
close(sockfd);
}
iOS: 如何获取ios设备的当前IP地址

将头文件导入ViewController.m中进行测试

iOS: 如何获取ios设备的当前IP地址
//
// ViewController.m
// IP_Test
//
// Created by mac on 16/7/15.
// Copyright © 2016年 xiayuanquan. All rights reserved.
//

#import "ViewController.h"
#import "IPAddress.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self getIPAddress];
}

//获取ip地址
- (void)getIPAddress
{
InitAddresses();
GetIPAddresses();
GetHWAddresses();

int i;
// NSString *deviceIP = nil;
for (i=0; i<MAXADDRS; ++i)
{
static unsigned long localHost = 0x7F000001; // 127.0.0.1
unsigned long theAddr;

theAddr
= ip_addrs[i];

if (theAddr == 0) break;
if (theAddr == localHost) continue;

NSLog(
@"Name: %s MAC: %s IP: %s\n", if_names[i], hw_addrs[i], ip_names[i]);
}
}

@end
iOS: 如何获取ios设备的当前IP地址

测试结果:

模拟器上连接wifi:

2016-07-15 16:19:49.187 IP_Test[5674:205359] Name: lo0  MAC: 00:00:00:00:00:00  IP: 127.0.0.1
2016-07-15 16:19:49.187 IP_Test[5674:205359] Name: en1 MAC: BC:54:36:CC:9C:96 IP: 192.168.0.109

 iphone上:

  连接wifi时:

2016-07-15 17:32:08.884 IP_Test[4813:2032752] Name: lo0  MAC: 00:00:00:00:00:00  IP: 127.0.0.1
2016-07-15 17:32:08.885 IP_Test[4813:2032752] Name: pdp_ip0 MAC: 00:00:00:00:00:70 IP: 10.204.102.136
2016-07-15 17:32:08.885 IP_Test[4813:2032752] Name: en0 MAC: 02:00:00:00:00:00 IP: 192.168.0.111

  使用4G时:

2016-07-15 17:32:44.163 IP_Test[4817:2033214] Name: lo0  MAC: 00:00:00:00:00:00  IP: 127.0.0.1
2016-07-15 17:32:44.164 IP_Test[4817:2033214] Name: pdp_ip0 MAC: 00:00:00:00:00:70 IP: 10.204.102.136

 

方法二:直接在ViewController.m中写获取ip地址的方法,该方法简单,而且能实时监测IP地址的变化

*参考地址为:http://*.com/questions/7072989/iphone-ipad-how-to-get-my-ip-address-programmatically

iOS: 如何获取ios设备的当前IP地址
//
// ViewController.m
// IP_Test
//
// Created by mac on 16/7/15.
// Copyright © 2016年 xiayuanquan. All rights reserved.
//

#import "ViewController.h"


#include
<ifaddrs.h>
#include
<arpa/inet.h>
#include
<net/if.h>

#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSLog(
@"%@",[self getIPAddresses]);
}


- (NSString *)getIPAddress:(BOOL)preferIPv4
{
NSArray
*searchArray = preferIPv4 ?
@[ IOS_VPN
@"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
@[ IOS_VPN
@"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;

NSDictionary
*addresses = [self getIPAddresses];
NSLog(
@"addresses: %@", addresses);

__block NSString
*address;
[searchArray enumerateObjectsUsingBlock:
^(NSString *key, NSUInteger idx, BOOL *stop)
{
address
= addresses[key];
if(address) *stop = YES;
} ];
return address ? address : @"0.0.0.0";
}
- (NSDictionary *)getIPAddresses
{
NSMutableDictionary
*addresses = [NSMutableDictionary dictionaryWithCapacity:8];

// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString
*name = [NSString stringWithUTF8String:interface->ifa_name];
NSString
*type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type
= IP_ADDR_IPv4;
}
}
else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type
= IP_ADDR_IPv6;
}
}
if(type) {
NSString
*key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key]
= [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}

@end
iOS: 如何获取ios设备的当前IP地址

测试结果:

模拟器上连接wifi:

iOS: 如何获取ios设备的当前IP地址
2016-07-15 16:23:35.864 IP_Test[5718:207702] {
"awdl0/ipv6" = "fe80::e863:edff:fe93:a1c2";
"en1/ipv4" = "192.168.0.109";
"en1/ipv6" = "fe80::be54:36ff:fecc:9c96";
"lo0/ipv4" = "127.0.0.1";
"lo0/ipv6" = "fe80::1";
}
2016-07-15 17:24:12.877 IP_Test[4796:2028704] 0.0.0.0
iOS: 如何获取ios设备的当前IP地址

iphone上:

  连接wifi时:

iOS: 如何获取ios设备的当前IP地址
2016-07-15 17:29:22.526 IP_Test[4807:2031207] addresses: {
"awdl0/ipv6" = "fe80::6c6a:24ff:fe91:39f7";
"en0/ipv4" = "192.168.0.111";
"en0/ipv6" = "fe80::42d:42b4:82a7:3de8";
"lo0/ipv4" = "127.0.0.1";
"lo0/ipv6" = "fe80::1";
"pdp_ip0/ipv4" = "10.204.102.136";
}
2016-07-15 17:29:22.527 IP_Test[4807:2031207] 192.168.0.111
iOS: 如何获取ios设备的当前IP地址

  使用4G时:

iOS: 如何获取ios设备的当前IP地址
2016-07-15 17:24:12.875 IP_Test[4796:2028704] addresses: {
"lo0/ipv4" = "127.0.0.1";
"lo0/ipv6" = "fe80::1";
"pdp_ip0/ipv4" = "10.204.102.136";
}
2016-07-15 17:24:12.877 IP_Test[4796:2028704] 10.204.102.136
iOS: 如何获取ios设备的当前IP地址

 

Github:https://github.com/xiayuanquan/IP_Test