ios AppIcon国际化

iOS AppIcon 本身无法国际化(即不能根据用户语言切换 App 图标内容),但你可以使用 Apple 提供的 Alternate Icons(替代图标)机制 来变通实现部分“图标国际化”的效果。

✅ iOS 不支持直接“AppIcon 国际化”

App Store 显示的图标始终使用主图标(AppIcon),不能因语言变化而变更。但可借助“替代图标”在应用内部进行切换(仅当用户首次打开应用后可改变图标)。

✅ 替代方案:使用 Alternate App Icons

你可以为不同语言或地区提供不同的 图标版本,在用户首次打开 App 后,根据语言设置自动切换。

  1. 设置 App 图标资源

在 Assets.xcassets 中:
• 创建主图标组 AppIcon
• 添加备用图标:创建新的 AppIcon Sets,如:
• AppIcon_EN
• AppIcon_ZH
• AppIcon_JP

确保每组图标包含必要的尺寸(iPhone/iPad/App Store 图标等)

  1. 修改 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)")
                }
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容