NSIcon provides a easy-to-use Mac app icon view for SwiftUI programming, reducing the need for manually storing additional resources by utilizing high-definition assets from the system's native resources.
Using NSIcon
is as simple as the built-in Image
view in SwiftUI.
You can get access to almost any app icon installed on your Mac.
// Use `init()` if you want to access the icon of your app itself.
NSIcon()
// Get an app icon view by name.
NSIcon("Safari")
// You can also access app icon by its bundle identifier.
NSIcon(bundleIdentifier: "com.apple.safari")
If the corresponding icon cannot be provided, NSIcon displays the GenericApplicationIcon that comes with macOS by default.
You can use iconPlaceholderStyle
modifier to get a different appearance of the placeholder.
NSIcon("unknown")
.iconPlaceholderStyle(.classic)
Considering the app sandbox environment on these platforms, the icon file assets of other apps are inaccessible, UIIcon
only provides the ability to access icon of the app itself.
Unlike the behavior of NSIcon
, UIIcon
adds mask to the icon by default. Use parameter addMask: Bool
to control.
On different platforms, the appearance of masks are different.
Platform | iOS | iPadOS | Mac Catalyst | watchOS | visionOS |
---|---|---|---|---|---|
Mask | AppIconMask | AppIconMask | MacAppIconMask | Circle | Circle |
UIIcon()
UIIcon(addMask: false)
In visionOS, the app icon consists of three different layers. Use init()
to render a merged version by default.
You can also use init(_ layer: AppIconlayer)
or init(_ layers: [AppIconlayer])
to select which parts of the icon to display.
UIIcon(.back)
UIIcon([.middle, .front])
Note:
UIIcon
does not supporticonPlaceholderStyle
modifier.
NSAsyncIcon
behaves similarly to NSIcon
, it obtains app icon from the App Store by accessing iTunes Search API.
NSAsyncIcon("Pages")
NSAsyncIcon(bundleIdentifier: "com.apple.iwork.pages")
When using appName
as an initialization parameter, you can set your preferences for iOS, macOS, watchOS or visionOS app to decide the results you receive. The default is .macOS
.
NSAsyncIcon("Pages", for: .iOS)
iOS app icons and a few of macOS app icons present in a opaque square shape. Therefore, consider whether to add a rounded rectangle mask to it. The default is false
.
Note:
NSAsyncIcon
will check if this icon contains transparent pixels. If so, the mask will not be added to the view.
NSAsyncIcon("Pages", for: .iOS, addMask: true)
Note: The iOS app icons appear slightly larger than Mac app icons, as determined by Apple's Human Interface Guidelines. This framework does not intend to undermine these rules. Please handle it according to your specific use case as needed.
Sometimes, you may want to access certain apps that are only available in specific countries or regions' App Store. You can easily achieve this by inputting a country code.
NSAsyncIcon("原神", country: "CN")
UIAsyncIcon
works almost exactly the same as NSAsyncIcon
, check the differences below:
- Add a mask to the icon by default
for platform: AppPlatform
parameter defaults to.iOS
- Support custom placeholder
- The
placeholderStyle
modifier is not supported
UIAsyncIcon
uses a ProgressView()
as the default placeholder, to create a custom placeholder, just add a custom view to the placeholder
closure.
UIAsyncIcon("Pages") { CustomPlaceholder() }
NSIcon is available with Swift Package Manager.
dependencies: [
.package(url: "https://github.com/Stv-X/NSIcon.git", branch: "main")
]