Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ https://github.com/danielsaidi/AppIconKit.git

## Support My Work

By [becoming a sponsor][Sponsors], you directly support the development & improvement of my various [open-source projects][OpenSource].

Every sponsor makes a real difference. Thank you for considering [supporting my work][Sponsors]!
Maintaining my various [open-source tools][OpenSource] takes significant time and effort. You can [become a sponsor][Sponsors] to help me dedicate more time to creating, maintaining, and improving these projects. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed. Thank you for considering!



Expand Down
28 changes: 18 additions & 10 deletions Sources/AppIconKit/AlternateAppIconContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import SwiftUI
/// This observable context class can be used to managed the
/// alternate app icon for an iOS or macOS app.
@MainActor
public class AlternateAppIconContext: ObservableObject {
open class AlternateAppIconContext: NSObject, ObservableObject {

/// Create an alternate app icon context instance.
public init() {
#if os(macOS)
public override init() {
super.init()
#if os(macOS) || targetEnvironment(macCatalyst)
guard let alternateAppIconName else { return }
setAlternateAppIconName(alternateAppIconName)
#endif
Expand All @@ -25,29 +26,36 @@ public class AlternateAppIconContext: ObservableObject {
/// The currently set alternate app icon name.
@AppStorage("com.danielsaidi.appiconkit.alternateappiconname")
public private(set) var alternateAppIconName: String?
}

public extension AlternateAppIconContext {

/// Reset the alternate app icon.
func resetAlternateAppIcon() {
open func resetAlternateAppIcon() {
guard alternateAppIconName != nil else { return }
setAlternateAppIconName(nil)
}

/// Set a certain alternate app icon.
func setAlternateAppIcon(
open func setAlternateAppIcon(
_ icon: AlternateAppIcon
) {
setAlternateAppIconName(icon.appIconName)
}

/// Set an alternate app icon with a certain name.
func setAlternateAppIconName(
open func setAlternateAppIconName(
_ name: String?
) {
alternateAppIconName = name
#if os(iOS) || os(tvOS)
#if targetEnvironment(macCatalyst)
if let nsApplication = NSClassFromString("NSApplication") as? NSObject.Type,
let shared = nsApplication.value(forKey: "sharedApplication") as? NSObject {
if let name, let imagePerform = Bundle.main.perform(NSSelectorFromString("imageForResource:"), with: name), let image = imagePerform.takeUnretainedValue() as? NSObject {
shared.setValue(image, forKey: "applicationIconImage")
} else {
alternateAppIconName = nil
shared.setValue(nil, forKey: "applicationIconImage")
}
}
#elseif os(iOS) || os(tvOS)
UIApplication.shared.setAlternateIconName(name)
#elseif os(macOS)
if let name {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppIconKit/AppIconKit.docc/AppIconKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ https://github.com/danielsaidi/AppIconKit.git

## Support My Work

By [becoming a sponsor][Sponsors], you directly support the development & improvement of my various [open-source projects][OpenSource].
Maintaining my various [open-source tools][OpenSource] takes significant time and effort. You can [become a sponsor][Sponsors] to help me dedicate more time to creating, maintaining, and improving these projects. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed. Thank you for considering!



Expand Down