【发布时间】:2016-10-25 07:50:59
【问题描述】:
我想知道IP地址的国家代码。
并且不使用 GPS。
有可能吗?
【问题讨论】:
-
你试过
NSLocale吗?
标签: ios ip-address country-codes
我想知道IP地址的国家代码。
并且不使用 GPS。
有可能吗?
【问题讨论】:
NSLocale吗?
标签: ios ip-address country-codes
您可以从http://lite.ip2location.com/database-ip-country下载IP到国家数据库
要从 IP 范围中提取国家别忘了计算 IP 号:
public Long getIpNumber(String ip) {
if (StringUtils.isBlank(ip))
return null;
String[] ipArray = ip.split("\\.");
long result = 0;
long ipLong = 0;
for (int x = 3; x >= 0; x--) {
ipLong = Long.parseLong(ipArray[3 - x]);
result |= ipLong << (x << 3);
}
return result;
}
【讨论】:
我认为NSLocale 是获取国家代码和名称而不是 IP 的好选择,因此请尝试如下代码:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
NSLog(@"%@ %@",countryName, countryCode);
希望对您有所帮助。
【讨论】: