You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS17.4.1更新后iPhone端地区名称显示异常技术问询

iOS 17.4.1升级后地区名称显示异常问题解决

问题描述

  • 将iOS版本从17.1.2升级至17.4.1后,App内TableView的部分地区名称显示异常:中国台湾地区显示为Root(Taiwn),中国澳门地区显示为Root(Makao),中国香港地区及中国内地也存在同类显示问题。
  • 相关代码未做任何修改,此前已稳定运行一年以上,原代码实现如下:
private func createCountriesByLang(lang: Langs) -> [CountryModel] {
    var result = [CountryModel]()
    for code in NSLocale.isoCountryCodes {
        let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
        let name = NSLocale(localeIdentifier: lang.rawValue).displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
        result.append(CountryModel(name: name, code: code))
    }
    switch lang {
    case Langs.en:
        result.append(CountryModel(name: "Zaire", code: "ZR"))
        result.append(CountryModel(name: "British Indian Ocean Territory", code: "IO"))
        result.append(CountryModel(name: "Netherlands Antilles", code: "AN"))
    case Langs.ru:
        result.append(CountryModel(name: "Заир", code: "ZR"))
        result.append(CountryModel(name: "Британская Территория в Индийском Океане", code: "IO"))
        result.append(CountryModel(name: "Нидерландские Антильские о-ва", code: "AN"))
    case Langs.zh:
        result.append(CountryModel(name: "扎伊尔", code: "ZR"))
        result.append(CountryModel(name: "英属印度洋领地", code: "IO"))
        result.append(CountryModel(name: "荷属安的列斯", code: "AN"))
    }
    return result
}

问题原因

苹果在iOS 17.4版本对NSLocale的地区名称获取逻辑进行了调整,原代码中通过displayName(forKey: .identifier, value: id)的方式不再适配新系统的地区数据结构,导致返回带有Root前缀的异常名称。

解决方案

替换原有的名称获取逻辑,改用displayName(forKey: .countryCode, value: code)直接通过ISO国家码获取对应语言下的地区名称,同时针对中国港澳台及内地地区手动指定规范名称,避免系统返回异常值。

修改后的代码如下:

private func createCountriesByLang(lang: Langs) -> [CountryModel] {
    var result = [CountryModel]()
    for code in NSLocale.isoCountryCodes {
        let name: String
        // 直接通过ISO国家码获取对应语言的地区名称
        if let displayName = NSLocale(localeIdentifier: lang.rawValue).displayName(forKey: .countryCode, value: code) {
            // 手动指定特殊地区的规范名称
            switch code {
            case "TW":
                name = "中国台湾"
            case "HK":
                name = "中国香港"
            case "MO":
                name = "中国澳门"
            case "CN":
                name = "中国内地"
            default:
                name = displayName
            }
        } else {
            name = "Country not found for code: \(code)"
        }
        result.append(CountryModel(name: name, code: code))
    }
    // 保留原有追加旧地区数据的逻辑
    switch lang {
    case Langs.en:
        result.append(CountryModel(name: "Zaire", code: "ZR"))
        result.append(CountryModel(name: "British Indian Ocean Territory", code: "IO"))
        result.append(CountryModel(name: "Netherlands Antilles", code: "AN"))
    case Langs.ru:
        result.append(CountryModel(name: "Заир", code: "ZR"))
        result.append(CountryModel(name: "Британская Территория в Индийском Океане", code: "IO"))
        result.append(CountryModel(name: "Нидерландские Антильские о-ва", code: "AN"))
    case Langs.zh:
        result.append(CountryModel(name: "扎伊尔", code: "ZR"))
        result.append(CountryModel(name: "英属印度洋领地", code: "IO"))
        result.append(CountryModel(name: "荷属安的列斯", code: "AN"))
    }
    return result
}

补充说明

  • 使用.countryCode作为key获取地区名称是苹果官方推荐的标准方式,能够适配iOS 17.4及后续版本的地区数据格式。
  • 手动指定中国港澳台及内地的名称,既保证了显示的规范性,也彻底规避了系统返回异常值的问题。

内容的提问来源于stack exchange,提问作者Миша Гарлинский

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:59:50