2016-06-23 43 views

回答

0

我觉得NSLocale是很好的选择,以获得国家代码和名称而不是IP所以尽量像下面的代码:

NSLocale *locale = [NSLocale currentLocale]; 
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode]; 
NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode value: countryCode]; 
NSLog(@"%@ %@",countryName, countryCode); 

希望这有助于你。

+0

是的,但它通过更改区域中的设置进行更改。 –

+0

然后这个免费的数据库可能会帮助你:http://dev.maxmind.com/geoip/legacy/geolite/ –

0

您可以从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; 
}