|
7 | 7 | // |
8 | 8 |
|
9 | 9 | #import "VPUPIPAddressUtil.h" |
| 10 | +#import "VPUPNetworkReachabilityManager.h" |
10 | 11 | #include <stdio.h> |
11 | 12 | #include <stdlib.h> |
12 | 13 | #include <string.h> |
|
34 | 35 | #pragma mark IPv4是32位地址长度 |
35 | 36 | #pragma mark IPv6是128位地址长度 |
36 | 37 |
|
| 38 | +@interface VPUPIPAddressUtil() |
| 39 | + |
| 40 | +@property (nonatomic, copy) NSString *ipAddress; |
| 41 | +@property (nonatomic, assign) VPUPNetworkReachabilityStatus currentReachabilityStatus; |
| 42 | + |
| 43 | ++ (instancetype)sharedIPAddressUtil; |
| 44 | + |
| 45 | +@end |
| 46 | + |
37 | 47 | @implementation VPUPIPAddressUtil |
38 | 48 |
|
| 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 | + |
39 | 99 | + (NSString *)currentIpAddress { |
40 | | - return [self getIPAddress:YES]; |
| 100 | + |
| 101 | + if ([VPUPIPAddressUtil sharedIPAddressUtil].currentReachabilityStatus != [VPUPNetworkReachabilityManager sharedManager].currentReachabilityStatus) { |
| 102 | + [VPUPIPAddressUtil sharedIPAddressUtil].currentReachabilityStatus = [VPUPNetworkReachabilityManager sharedManager].currentReachabilityStatus; |
| 103 | + [VPUPIPAddressUtil sharedIPAddressUtil].ipAddress = [VPUPIPAddressUtil deviceWANIPAddress]; |
| 104 | + } |
| 105 | + |
| 106 | + if ([VPUPIPAddressUtil sharedIPAddressUtil].ipAddress.length < 1) { |
| 107 | + [VPUPIPAddressUtil sharedIPAddressUtil].ipAddress = [VPUPIPAddressUtil deviceWANIPAddress]; |
| 108 | + } |
| 109 | + return [VPUPIPAddressUtil sharedIPAddressUtil].ipAddress; |
41 | 110 | } |
42 | 111 |
|
43 | 112 | + (NSString *)getIPAddress:(BOOL)preferIPv4 { |
|
0 commit comments