如何获取本地化的CFBundleDisplayName

Umg*_*gre 30 bundle objective-c ios

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
Run Code Online (Sandbox Code Playgroud)

此API返回plist中的Bundle Display Name.

但是我的应用程序已本地化并具有不同的显示名称

所以我需要获得本地化的显示名称,InfoPlist.strings因为设备语言设置会有所不同.

jus*_*tin 61

你试过-[NSBundle localizedInfoDictionary]吗?

[[[NSBundle mainBundle] localizedInfoDictionary]
       objectForKey:@"CFBundleDisplayName"]
Run Code Online (Sandbox Code Playgroud)

  • 这个方法也更直接地返回一个本地化的字符串:`[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]` (10认同)

Kru*_*nal 5

为 Swift 试试这个:

// Localized
if let displayName = Bundle.main.localizedInfoDictionary?["CFBundleDisplayName"] as? String {
    print("App Display Name - \(displayName)")
}

// Non-Localized
if let displayName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String {
    print("App Display Name - \(displayName)")
}
Run Code Online (Sandbox Code Playgroud)

也试试这个,如果你还没有设置显示名称

// Localized
if let appName = Bundle.main. localizedInfoDictionary?["CFBundleName"] as? String {
    print("App Name - \(appName)")
}

// Non-Localized
if let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String {
    print("App Name - \(appName)")
}
Run Code Online (Sandbox Code Playgroud)

有用的技巧:

// Print bundle info dictionary to get complete details about app
print("Bundle.main.infoDictionary - \(Bundle.main.infoDictionary)")
print("Bundle.main.localizedInfoDictionary - \(Bundle.main.localizedInfoDictionary)")
Run Code Online (Sandbox Code Playgroud)