77//
88
99#import " VPUPIPAddressUtil.h"
10- #import " VPUPNetworkReachabilityManager.h"
1110#include < stdio.h>
1211#include < stdlib.h>
1312#include < string.h>
3534#pragma mark IPv4是32位地址长度
3635#pragma mark IPv6是128位地址长度
3736
38- @interface VPUPIPAddressUtil ()
39-
40- @property (nonatomic , copy ) NSString *ipAddress;
41- @property (nonatomic , assign ) VPUPNetworkReachabilityStatus currentReachabilityStatus;
42-
43- + (instancetype )sharedIPAddressUtil ;
44-
45- @end
46-
4737@implementation VPUPIPAddressUtil
4838
49-
50- + (instancetype )sharedIPAddressUtil {
51- static VPUPIPAddressUtil *_sharedIPAddressUtil = nil ;
52- static dispatch_once_t onceToken;
53- dispatch_once (&onceToken, ^{
54- _sharedIPAddressUtil = [[self alloc ] init ];
55- _sharedIPAddressUtil.currentReachabilityStatus = [VPUPNetworkReachabilityManager sharedManager ].currentReachabilityStatus ;
56- // _sharedIPAddressUtil.ipAddress = [VPUPIPAddressUtil deviceWANIPAddress];
57- });
58- return _sharedIPAddressUtil;
59- }
60-
61- // 通过搜狐接口获取外网IP
62- + (NSString *)getWANIPAddress {
63- NSError *error;
64- NSURL *ipURL = [NSURL URLWithString: @" http://pv.sohu.com/cityjson?ie=utf-8" ];
65-
66- NSMutableString *ip = [NSMutableString stringWithContentsOfURL: ipURL encoding: NSUTF8StringEncoding error: &error];
67- // 判断返回字符串是否为所需数据
68- if ([ip hasPrefix: @" var returnCitySN = " ]) {
69- // 对字符串进行处理,然后进行json解析
70- // 删除字符串多余字符串
71- NSRange range = NSMakeRange (0 , 19 );
72- [ip deleteCharactersInRange: range];
73- NSString * nowIp =[ip substringToIndex: ip.length-1 ];
74- // 将字符串转换成二进制进行Json解析
75- NSData * data = [nowIp dataUsingEncoding: NSUTF8StringEncoding];
76- NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil ];
77- NSLog (@" %@ " ,dict);
78- return dict[@" cip" ] ? dict[@" cip" ] : @" " ;
79- }
80- return @" " ;
81- }
82-
83- // 通过淘宝接口获取外网IP
84- + (NSString *)deviceWANIPAddress {
85-
86- NSURL *ipURL = [NSURL URLWithString: @" http://ip.taobao.com/service/getIpInfo.php?ip=myip" ];
87- NSData *data = [NSData dataWithContentsOfURL: ipURL];
88- if (!data) {
89- return @" " ;
90- }
91- NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil ];
92- NSString *ipStr = nil ;
93- if (ipDic && [ipDic[@" code" ] integerValue ] == 0 ) { // 获取成功
94- ipStr = ipDic[@" data" ][@" ip" ];
95- }
96- return (ipStr ? ipStr : @" " );
97- }
98-
9939+ (NSString *)currentIpAddress {
100-
101- return [self getIPAddress: YES ];
102-
103- // if ([VPUPIPAddressUtil sharedIPAddressUtil].currentReachabilityStatus != [VPUPNetworkReachabilityManager sharedManager].currentReachabilityStatus) {
104- // [VPUPIPAddressUtil sharedIPAddressUtil].currentReachabilityStatus = [VPUPNetworkReachabilityManager sharedManager].currentReachabilityStatus;
105- // [VPUPIPAddressUtil sharedIPAddressUtil].ipAddress = [VPUPIPAddressUtil deviceWANIPAddress];
106- // }
107- //
108- // if ([VPUPIPAddressUtil sharedIPAddressUtil].ipAddress.length < 1) {
109- // [VPUPIPAddressUtil sharedIPAddressUtil].ipAddress = [VPUPIPAddressUtil deviceWANIPAddress];
110- // }
40+ return [self getIPAddress: YES ];
11141}
11242
11343+ (NSString *)getIPAddress : (BOOL )preferIPv4 {
@@ -123,7 +53,7 @@ + (NSString *)getIPAddress:(BOOL)preferIPv4 {
12353 if (address) {
12454 *stop = YES ;
12555 }
126- }];
56+ }];
12757 return address ? address : @" 0.0.0.0" ;
12858}
12959
0 commit comments