Skip to content

Commit

Permalink
doc: update example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 1, 2024
1 parent ea1c694 commit b382112
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 8 additions & 0 deletions DeviceInfoExample/DeviceInfoExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ struct ContentView: View {
var body: some View {

List {
#if os(macOS)
Image(nsImage: DeviceInfo.appIcon)
#endif
#if os(iOS)
if let icon = DeviceInfo.appIcon {
Image(uiImage: icon)
}
#endif
LabelCaption(content: "bundleName") {
Text("\(DeviceInfo.bundleName)")
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/DeviceInfo/DeviceInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public struct DeviceInfo {
#endif

#if canImport(UIKit)
static let appIcon: UIImage? = getAppIcon()
static func getAppIcon() -> UIImage? {
if let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIconsDictionary = iconsDictionary["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIconsDictionary["CFBundleIconFiles"] as? [String],
let lastIcon = iconFiles.last {
@MainActor
public static let appIcon: UIImage? = {
if let icons = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
let lastIcon = iconFiles.last {
return UIImage(named: lastIcon)
}
return nil
}
}()
#elseif canImport(AppKit)
@MainActor
static let appIcon: NSImage = NSApp.applicationIconImage!
public static let appIcon: NSImage = NSApp.applicationIconImage!
#elseif canImport(WatchKit)
#endif
}
Expand Down

0 comments on commit b382112

Please sign in to comment.