Alternate App Icons
An amazing addition to an app are color themes. Setting alternate App Icons is an excellent addition to that.
Apple provides the function `setAlternateIconName` to do that. There are some limitations, so one cannot update the icon too frequently.
For the example two icons were created with Icon Composer. One is set as the standard app icon and the other is added to the build setting `ASSETCATALOG_COMPILER_ALTERNATE_APPICON_NAMES` ("Alternate App Icon Sets").
class ThemeManager: ObservableObject {
@Published var currentTheme: AppTheme = .standard
private var timerCancellable: AnyCancellable?
init() {
let themeName = UserDefaults.standard.object(forKey: "currentTheme") as? String ?? "standard"
let theme = AppTheme(rawValue: themeName) ?? .standard
currentTheme = theme
}
func updateTheme(_ theme: AppTheme) {
currentTheme = theme
UserDefaults.standard.set(currentTheme.rawValue, forKey: "currentTheme")
updateAppIcon()
}
func updateAppIcon() {
switch currentTheme {
case .standard:
UIApplication.shared.setAlternateIconName(nil, completionHandler: { error in
if let error {
print("error: \(String(describing: error))")
}
})
case .christmas:
UIApplication.shared.setAlternateIconName("AppIcon-Christmas", completionHandler: { error in
if let error {
print("error: \(String(describing: error))")
}
})
}
}
}