一个简单的 Swift 实用工具,用于管理和检索 iOS 应用程序图标的详细信息。
AppIcon
结构体提供了静态属性和函数,便于与应用程序的图标设置进行无缝交互。它具有检查备用图标支持、确定当前图标、获取所有已定义的图标以及设置备用图标的功能。
您可以使用 Swift Package Manager 将 AppIcon
作为依赖项添加到您的项目中,方法是将其添加到 Package.swift
的 dependencies 值中。
dependencies: [
.package(url: "https://github.com/kevinhermawan/AppIcon.git", .upToNextMajor(from: "2.0.0"))
]
或者,在 Xcode 中
File
-> Swift Packages
-> Add Package Dependency...
https://github.com/kevinhermawan/AppIcon.git
Add Package
。import AppIcon
// Checking if alternate icons are supported
if AppIcon.isSupported {
print("Alternate icons are supported!")
} else {
print("Alternate icons are not supported.")
}
// Fetching the current app icon details
if let currentIcon = AppIcon.current {
print("Current app icon: \(currentIcon.label) - \(currentIcon.iconName)")
} else {
print("Using default app icon")
}
// Fetching all defined icons
for icon in AppIcon.defined {
print("\(icon.label) - \(icon.iconName)")
}
// Retrieving a specific icon by its iconName
let iconName = "Alternate1"
if let specificIcon = AppIcon.icon(for: iconName) {
print("Found icon: \(specificIcon.label) - \(specificIcon.iconName)")
} else {
print("Icon not found for name \(iconName)")
}
// Setting an alternate app icon (for example, "Alternate1")
if let iconToSet = AppIcon.defined.first(where: { $0.iconName == "Alternate1" }) {
AppIcon.set(to: iconToSet) { error in
if let error = error {
print("Failed to set app icon: \(error.localizedDescription)")
} else {
print("App icon successfully set!")
}
}
} else {
print("Failed to find the specified icon.")
}