2016-06-23 103 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; 
}