【问题标题】:How the country code with the ip address?国码怎么加上ip地址?
【发布时间】:2016-10-25 07:50:59
【问题描述】:

我想知道IP地址的国家代码。

并且不使用 GPS。

有可能吗?

【问题讨论】:

  • 你试过NSLocale吗?

标签: ios ip-address country-codes


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我认为NSLocale 是获取国家代码和名称而不是 IP 的好选择,因此请尝试如下代码:

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

    希望对您有所帮助。

    【讨论】:

    猜你喜欢
    • 2014-10-21
    • 2011-12-12
    • 2015-05-15
    • 2014-03-23
    • 2013-07-02
    • 2015-03-15
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多