Reprint-ios Get device IP address

Source: Internet
Author: User

iOS Get device IP address

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <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"


#pragma mark-Get the device's current network IP address
+ (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_CELLU LAR @ "/" 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_CELLU LAR @ "/" 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];
Filter out IP address formats
if ([self isvalidatip:address]) *stop = YES;
} ];
Return address? Address: @ "0.0.0.0";
}

+ (BOOL) Isvalidatip: (NSString *) ipAddress {
if (Ipaddress.length = = 0) {
return NO;
}
NSString *urlregex = @ "^ ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "
"([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "
"([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \. "
"([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) $ ";

Nserror *error;
Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:urlregex options:0 error:&error];

if (regex! = nil) {
Nstextcheckingresult *firstmatch=[regex firstmatchinstring:ipaddress options:0 range:NSMakeRange (0, [ipAddress Length]);

if (Firstmatch) {
Nsrange ResultRange = [Firstmatch rangeatindex:0];
NSString *result=[ipaddress Substringwithrange:resultrange];
Output results
NSLog (@ "%@", result);
return YES;
}
}
return NO;
}

+ (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;
}
Original link Address: http://SuperDanny.link/2016/01/27/iOS-get-device-ip-address/index.html

Reprint-ios Get device IP address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.