Write in front
Recently do about the network Interface section, sometimes for the purpose of interface security, need to request the interface at the time, with the post-service side about good, each interface needs to pass specific signature data, about the signature algorithm, the server and the mobile side is consistent; Of course this is not the main, the main thing is, how to get the IP address of the device directly on the code;
Small tips:
MAC terminal get PC Ip:ifconfig | grep "inet" | Grep-v 127.0.0.1
Main reference: StackOverflow
1 http://stackoverflow.com/questions/7072989/ iphone-ipad-osx-how-to-get-my-ip-address-programmatically/10803584#10803584
1 Import Header File2#include <ifaddrs.h>3#include <arpa/inet.h>4#include <net/if.h>5 6 #defineIos_cellular @ "Pdp_ip0"7 #defineIos_wifi @ "En0"8 #defineIos_vpn @ "Utun0"9 #defineIp_addr_ipv4 @ "IPv4"Ten #defineIp_addr_ipv6 @ "IPv6" One A - - #pragmaMark-Get IP address the+ (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]; + A //NSLog (@ "addresses:%@", addresses); at -__block NSString *address; -[Searcharray enumerateobjectsusingblock:^ (NSString *key, Nsuinteger idx, BOOL *stop) - { -Address =Addresses[key]; - //filter out IP address formats in if([self isvalidatip:address]) *stop =YES; - } ]; to returnAddress? Address:@"0.0.0.0"; + - } the *+ (BOOL) Isvalidatip: (NSString *) ipAddress { $ if(Ipaddress.length = =0) {Panax Notoginseng returnNO; - } theNSString *urlregex =@"^ ([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \." + "([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \." A "([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) \ \." the "([01]?\\d\\d?| 2[0-4]\\D|25[0-5]) $"; + -Nserror *error; $Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:urlregex options:0error:&ERROR]; $ - if(Regex! =Nil) { -Nstextcheckingresult *firstmatch=[regex firstmatchinstring:ipaddress Options:0Range:nsmakerange (0, [ipAddress length])]; the - if(firstmatch) {WuyiNsrange ResultRange = [Firstmatch rangeatindex:0]; theNSString *result=[ipAddress Substringwithrange:resultrange]; - //Output Results Wu //NSLog (@ "%@", result); - returnYES; About } $ } - returnNO; - } - A+ (Nsdictionary *) getipaddresses + { theNsmutabledictionary *addresses = [Nsmutabledictionary dictionarywithcapacity:8]; - $ //retrieve the current interfaces-returns 0 on success the structIfaddrs *interfaces; the if(!getifaddrs (&interfaces)) { the //Loop through linked list of interfaces the structIfaddrs *Interface; - for(Interface=interfaces;Interface;Interface=Interface-Ifa_next) { in if(! (Interface->ifa_flags & Iff_up)/* || (Interface->ifa_flags & Iff_loopback)*/ ) { the Continue;//deeply nested code harder to read the } About Const structsockaddr_in *addr = (Const structsockaddr_in*)Interface-ifa_addr; the Charaddrbuf[MAX (Inet_addrstrlen, Inet6_addrstrlen)]; the if(Addr && (addr->sin_family==af_inet | | addr->sin_family==Af_inet6)) { theNSString *name = [NSString stringwithutf8string:Interface-Ifa_name]; +NSString *type; - if(addr->sin_family = =af_inet) { the if(Inet_ntop (Af_inet, &addr->sin_addr, Addrbuf, Inet_addrstrlen)) {BayiType =Ip_addr_ipv4; the } the}Else { - Const structSockaddr_in6 *ADDR6 = (Const structsockaddr_in6*)Interface-ifa_addr; - if(Inet_ntop (Af_inet6, &addr6->sin6_addr, Addrbuf, Inet6_addrstrlen)) { theType =Ip_addr_ipv6; the } the } the if(type) { -NSString *key = [NSString stringWithFormat:@"%@/%@", name, type]; theAddresses[key] =[NSString stringwithutf8string:addrbuf]; the } the }94 } the //Free Memory the Freeifaddrs (interfaces); the }98 return[Addresses Count]?Addresses:nil; About}
Pro-Test available, omnipotent StackOverflow, thank you very much!
Get the iOS device IP address