iOS AppIcon 本身无法国际化(即不能根据用户语言切换 App 图标内容),但你可以使用 Apple 提供的 Alternate Icons(替代图标)机制 来变通实现部分“图标国际化”的效果。
⸻
✅ iOS 不支持直接“AppIcon 国际化”
App Store 显示的图标始终使用主图标(AppIcon),不能因语言变化而变更。但可借助“替代图标”在应用内部进行切换(仅当用户首次打开应用后可改变图标)。
⸻
✅ 替代方案:使用 Alternate App Icons
你可以为不同语言或地区提供不同的 图标版本,在用户首次打开 App 后,根据语言设置自动切换。
- 设置 App 图标资源
在 Assets.xcassets 中:
• 创建主图标组 AppIcon
• 添加备用图标:创建新的 AppIcon Sets,如:
• AppIcon_EN
• AppIcon_ZH
• AppIcon_JP
确保每组图标包含必要的尺寸(iPhone/iPad/App Store 图标等)
- 修改 Info.plist 添加替代图标配置
<key>CFBundleIcons</key>
<dict>
<key>CFBundlePrimaryIcon</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>EN</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon_EN</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
<key>ZH</key>
<dict>
<key>CFBundleIconFiles</key>
<array>
<string>AppIcon_ZH</string>
</array>
<key>UIPrerenderedIcon</key>
<false/>
</dict>
</dict>
</dict>
OC实现
#import <UIKit/UIKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self switchAppIconIfNeeded];
return YES;
}
- (void)switchAppIconIfNeeded {
if (![UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"不支持切换 App 图标");
return;
}
NSString *language = [[NSLocale preferredLanguages] firstObject];
NSString *iconName = nil;
if ([language hasPrefix:@"zh"]) {
iconName = @"ZH";
} else if ([language hasPrefix:@"en"]) {
iconName = @"EN";
} else {
iconName = nil; // 默认图标
}
NSString *currentIcon = [UIApplication sharedApplication].alternateIconName;
// 只在图标不同时才切换
if ((iconName == nil && currentIcon != nil) || ![iconName isEqualToString:currentIcon]) {
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@"切换 App 图标失败: %@", error.localizedDescription);
} else {
NSLog(@"成功切换图标为: %@", iconName ?: @"默认图标");
}
}];
}
}
swift
import UIKit
func setAppIconForCurrentLanguage() {
let preferredLang = Locale.preferredLanguages.first ?? "en"
var iconName: String?
if preferredLang.hasPrefix("zh") {
iconName = "ZH"
} else if preferredLang.hasPrefix("en") {
iconName = "EN"
} else {
iconName = nil // 默认图标
}
if UIApplication.shared.supportsAlternateIcons {
if UIApplication.shared.alternateIconName != iconName {
UIApplication.shared.setAlternateIconName(iconName) { error in
if let error = error {
print("切换图标失败:\(error.localizedDescription)")
}
}
}
}
}