1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="token comment">//</span> <span class="token comment">// ZBTool.h</span> <span class="token comment">// HTTPTransfer</span> <span class="token comment">//</span> <span class="token comment">// Created by 柏超曾 on 2017/8/28.</span> <span class="token comment">// Copyright © 2017年 柏超曾. All rights reserved.</span> <span class="token comment">//</span> #<span class="token keyword">import</span> <span class="token operator"><</span>Foundation<span class="token operator">/</span>Foundation<span class="token punctuation">.</span>h<span class="token operator">></span> @<span class="token keyword">interface</span> <span class="token class-name">ZBTool</span> <span class="token punctuation">:</span> NSObject <span class="token operator">+</span> <span class="token punctuation">(</span>NSString <span class="token operator">*</span><span class="token punctuation">)</span>getIPAddress<span class="token punctuation">:</span><span class="token punctuation">(</span><span class="token constant">BOOL</span><span class="token punctuation">)</span>preferIPv4<span class="token punctuation">;</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token constant">BOOL</span><span class="token punctuation">)</span>isValidatIP<span class="token punctuation">:</span><span class="token punctuation">(</span>NSString <span class="token operator">*</span><span class="token punctuation">)</span>ipAddress <span class="token punctuation">;</span> <span class="token operator">+</span> <span class="token punctuation">(</span>NSDictionary <span class="token operator">*</span><span class="token punctuation">)</span>getIPAddresses<span class="token punctuation">;</span> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<span class="token comment">//</span> <span class="token comment">// ZBTool.m</span> <span class="token comment">// HTTPTransfer</span> <span class="token comment">//</span> <span class="token comment">// Created by 柏超曾 on 2017/8/28.</span> <span class="token comment">// Copyright © 2017年 柏超曾. All rights reserved.</span> <span class="token comment">//</span> #<span class="token keyword">import</span> <span class="token string">"ZBTool.h"</span> #<span class="token keyword">import</span> <span class="token operator"><</span>ifaddrs<span class="token punctuation">.</span>h<span class="token operator">></span> #<span class="token keyword">import</span> <span class="token operator"><</span>arpa<span class="token operator">/</span>inet<span class="token punctuation">.</span>h<span class="token operator">></span> #<span class="token keyword">import</span> <span class="token operator"><</span>net<span class="token operator">/</span><span class="token keyword">if</span><span class="token punctuation">.</span>h<span class="token operator">></span> #define <span class="token constant">IOS_CELLULAR</span> @<span class="token string">"pdp_ip0"</span> #define <span class="token constant">IOS_WIFI</span> @<span class="token string">"en0"</span> #define <span class="token constant">IOS_VPN</span> @<span class="token string">"utun0"</span> #define IP_ADDR_IPv4 @<span class="token string">"ipv4"</span> #define IP_ADDR_IPv6 @<span class="token string">"ipv6"</span> @implementation ZBTool #pragma mark <span class="token operator">-</span> 获取设备当前网络<span class="token constant">IP</span>地址 <span class="token operator">+</span> <span class="token punctuation">(</span>NSString <span class="token operator">*</span><span class="token punctuation">)</span>getIPAddress<span class="token punctuation">:</span><span class="token punctuation">(</span><span class="token constant">BOOL</span><span class="token punctuation">)</span>preferIPv4 <span class="token punctuation">{</span> NSArray <span class="token operator">*</span>searchArray <span class="token operator">=</span> preferIPv4 <span class="token operator">?</span> @<span class="token punctuation">[</span> <span class="token constant">IOS_VPN</span> @<span class="token string">"/"</span> IP_ADDR_IPv4<span class="token punctuation">,</span> <span class="token constant">IOS_VPN</span> @<span class="token string">"/"</span> IP_ADDR_IPv6<span class="token punctuation">,</span> <span class="token constant">IOS_WIFI</span> @<span class="token string">"/"</span> IP_ADDR_IPv4<span class="token punctuation">,</span> <span class="token constant">IOS_WIFI</span> @<span class="token string">"/"</span> IP_ADDR_IPv6<span class="token punctuation">,</span> <span class="token constant">IOS_CELLULAR</span> @<span class="token string">"/"</span> IP_ADDR_IPv4<span class="token punctuation">,</span> <span class="token constant">IOS_CELLULAR</span> @<span class="token string">"/"</span> IP_ADDR_IPv6 <span class="token punctuation">]</span> <span class="token punctuation">:</span> @<span class="token punctuation">[</span> <span class="token constant">IOS_VPN</span> @<span class="token string">"/"</span> IP_ADDR_IPv6<span class="token punctuation">,</span> <span class="token constant">IOS_VPN</span> @<span class="token string">"/"</span> IP_ADDR_IPv4<span class="token punctuation">,</span> <span class="token constant">IOS_WIFI</span> @<span class="token string">"/"</span> IP_ADDR_IPv6<span class="token punctuation">,</span> <span class="token constant">IOS_WIFI</span> @<span class="token string">"/"</span> IP_ADDR_IPv4<span class="token punctuation">,</span> <span class="token constant">IOS_CELLULAR</span> @<span class="token string">"/"</span> IP_ADDR_IPv6<span class="token punctuation">,</span> <span class="token constant">IOS_CELLULAR</span> @<span class="token string">"/"</span> IP_ADDR_IPv4 <span class="token punctuation">]</span> <span class="token punctuation">;</span> NSDictionary <span class="token operator">*</span>addresses <span class="token operator">=</span> <span class="token punctuation">[</span>self getIPAddresses<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">NSLog</span><span class="token punctuation">(</span>@<span class="token string">"addresses: %@"</span><span class="token punctuation">,</span> addresses<span class="token punctuation">)</span><span class="token punctuation">;</span> __block NSString <span class="token operator">*</span>address<span class="token punctuation">;</span> <span class="token punctuation">[</span>searchArray enumerateObjectsUsingBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">(</span>NSString <span class="token operator">*</span>key<span class="token punctuation">,</span> NSUInteger idx<span class="token punctuation">,</span> <span class="token constant">BOOL</span> <span class="token operator">*</span>stop<span class="token punctuation">)</span> <span class="token punctuation">{</span> address <span class="token operator">=</span> addresses<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//筛选出IP地址格式</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">[</span>self isValidatIP<span class="token punctuation">:</span>address<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span>stop <span class="token operator">=</span> <span class="token constant">YES</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">return</span> address <span class="token operator">?</span> address <span class="token punctuation">:</span> @<span class="token string">"0.0.0.0"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token constant">BOOL</span><span class="token punctuation">)</span>isValidatIP<span class="token punctuation">:</span><span class="token punctuation">(</span>NSString <span class="token operator">*</span><span class="token punctuation">)</span>ipAddress <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ipAddress<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token constant">NO</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> NSString <span class="token operator">*</span>urlRegEx <span class="token operator">=</span> @<span class="token string">"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."</span> <span class="token string">"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."</span> <span class="token string">"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."</span> <span class="token string">"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"</span><span class="token punctuation">;</span> NSError <span class="token operator">*</span>error<span class="token punctuation">;</span> NSRegularExpression <span class="token operator">*</span>regex <span class="token operator">=</span> <span class="token punctuation">[</span>NSRegularExpression regularExpressionWithPattern<span class="token punctuation">:</span>urlRegEx options<span class="token punctuation">:</span><span class="token number">0</span> error<span class="token punctuation">:</span><span class="token operator">&</span>error<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>regex <span class="token operator">!=</span> nil<span class="token punctuation">)</span> <span class="token punctuation">{</span> NSTextCheckingResult <span class="token operator">*</span>firstMatch<span class="token operator">=</span><span class="token punctuation">[</span>regex firstMatchInString<span class="token punctuation">:</span>ipAddress options<span class="token punctuation">:</span><span class="token number">0</span> range<span class="token punctuation">:</span><span class="token function">NSMakeRange</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>ipAddress length<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>firstMatch<span class="token punctuation">)</span> <span class="token punctuation">{</span> NSRange resultRange <span class="token operator">=</span> <span class="token punctuation">[</span>firstMatch rangeAtIndex<span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> NSString <span class="token operator">*</span>result<span class="token operator">=</span><span class="token punctuation">[</span>ipAddress substringWithRange<span class="token punctuation">:</span>resultRange<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//输出结果</span> <span class="token function">NSLog</span><span class="token punctuation">(</span>@<span class="token string">"%@"</span><span class="token punctuation">,</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token constant">YES</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token constant">NO</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator">+</span> <span class="token punctuation">(</span>NSDictionary <span class="token operator">*</span><span class="token punctuation">)</span>getIPAddresses <span class="token punctuation">{</span> NSMutableDictionary <span class="token operator">*</span>addresses <span class="token operator">=</span> <span class="token punctuation">[</span>NSMutableDictionary dictionaryWithCapacity<span class="token punctuation">:</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// retrieve the current interfaces - returns 0 on success</span> struct ifaddrs <span class="token operator">*</span>interfaces<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">getifaddrs</span><span class="token punctuation">(</span><span class="token operator">&</span>interfaces<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Loop through linked list of interfaces</span> struct ifaddrs <span class="token operator">*</span><span class="token keyword">interface</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">interface</span><span class="token operator">=</span>interfaces<span class="token punctuation">;</span> <span class="token keyword">interface</span><span class="token punctuation">;</span> <span class="token keyword">interface</span><span class="token operator">=</span><span class="token keyword">interface</span><span class="token operator">-</span><span class="token operator">></span>ifa_next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span><span class="token keyword">interface</span><span class="token operator">-</span><span class="token operator">></span>ifa_flags <span class="token operator">&</span> <span class="token constant">IFF_UP</span><span class="token punctuation">)</span> <span class="token comment">/* || (interface->ifa_flags & IFF_LOOPBACK) */</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token comment">// deeply nested code harder to read</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> struct sockaddr_in <span class="token operator">*</span>addr <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">const</span> struct sockaddr_in<span class="token operator">*</span><span class="token punctuation">)</span><span class="token keyword">interface</span><span class="token operator">-</span><span class="token operator">></span>ifa_addr<span class="token punctuation">;</span> char addrBuf<span class="token punctuation">[</span> <span class="token constant">MAX</span><span class="token punctuation">(</span><span class="token constant">INET_ADDRSTRLEN</span><span class="token punctuation">,</span> <span class="token constant">INET6_ADDRSTRLEN</span><span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>addr <span class="token operator">&&</span> <span class="token punctuation">(</span>addr<span class="token operator">-</span><span class="token operator">></span>sin_family<span class="token operator">==</span><span class="token constant">AF_INET</span> <span class="token operator">||</span> addr<span class="token operator">-</span><span class="token operator">></span>sin_family<span class="token operator">==</span><span class="token constant">AF_INET6</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> NSString <span class="token operator">*</span>name <span class="token operator">=</span> <span class="token punctuation">[</span>NSString stringWithUTF8String<span class="token punctuation">:</span><span class="token keyword">interface</span><span class="token operator">-</span><span class="token operator">></span>ifa_name<span class="token punctuation">]</span><span class="token punctuation">;</span> NSString <span class="token operator">*</span>type<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>addr<span class="token operator">-</span><span class="token operator">></span>sin_family <span class="token operator">==</span> <span class="token constant">AF_INET</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">inet_ntop</span><span class="token punctuation">(</span><span class="token constant">AF_INET</span><span class="token punctuation">,</span> <span class="token operator">&</span>addr<span class="token operator">-</span><span class="token operator">></span>sin_addr<span class="token punctuation">,</span> addrBuf<span class="token punctuation">,</span> <span class="token constant">INET_ADDRSTRLEN</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> type <span class="token operator">=</span> IP_ADDR_IPv4<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">const</span> struct sockaddr_in6 <span class="token operator">*</span>addr6 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">const</span> struct sockaddr_in6<span class="token operator">*</span><span class="token punctuation">)</span><span class="token keyword">interface</span><span class="token operator">-</span><span class="token operator">></span>ifa_addr<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">inet_ntop</span><span class="token punctuation">(</span><span class="token constant">AF_INET6</span><span class="token punctuation">,</span> <span class="token operator">&</span>addr6<span class="token operator">-</span><span class="token operator">></span>sin6_addr<span class="token punctuation">,</span> addrBuf<span class="token punctuation">,</span> <span class="token constant">INET6_ADDRSTRLEN</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> type <span class="token operator">=</span> IP_ADDR_IPv6<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span> NSString <span class="token operator">*</span>key <span class="token operator">=</span> <span class="token punctuation">[</span>NSString stringWithFormat<span class="token punctuation">:</span>@<span class="token string">"%@/%@"</span><span class="token punctuation">,</span> name<span class="token punctuation">,</span> type<span class="token punctuation">]</span><span class="token punctuation">;</span> addresses<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>NSString stringWithUTF8String<span class="token punctuation">:</span>addrBuf<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">// Free memory</span> <span class="token function">freeifaddrs</span><span class="token punctuation">(</span>interfaces<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token punctuation">[</span>addresses count<span class="token punctuation">]</span> <span class="token operator">?</span> addresses <span class="token punctuation">:</span> nil<span class="token punctuation">;</span> <span class="token punctuation">}</span> @end |
作者:曾柏超
链接:https://www.jianshu.com/p/7872b43f0cc7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。