Skip to content

Commit 70e9998

Browse files
committed
fix ip address
1 parent cab7cc7 commit 70e9998

File tree

1 file changed

+70
-1
lines changed
  • VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/Common/Utils

1 file changed

+70
-1
lines changed

VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/Common/Utils/VPUPIPAddressUtil.m

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "VPUPIPAddressUtil.h"
10+
#import "VPUPNetworkReachabilityManager.h"
1011
#include <stdio.h>
1112
#include <stdlib.h>
1213
#include <string.h>
@@ -34,10 +35,78 @@
3435
#pragma mark IPv4是32位地址长度
3536
#pragma mark IPv6是128位地址长度
3637

38+
@interface VPUPIPAddressUtil()
39+
40+
@property (nonatomic, copy) NSString *ipAddress;
41+
@property (nonatomic, assign) VPUPNetworkReachabilityStatus currentReachabilityStatus;
42+
43+
+ (instancetype)sharedIPAddressUtil;
44+
45+
@end
46+
3747
@implementation VPUPIPAddressUtil
3848

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+
3999
+ (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;
41110
}
42111

43112
+ (NSString *)getIPAddress:(BOOL)preferIPv4 {

0 commit comments

Comments
 (0)