Get urls to higher quality website icons than /favicon.ico
Use with Package Manager.
import Favicon
let url = URL(string: "https://example.com")!
let favicon = Favicon(url: url)
let faviconUrl: URL? = await favicon.url() // Large size by default
You might want to skip icons with certain extensions.
For example SwiftUI doesn't render SVG images at the time of writing.
Use this constructor:
let favicon = Favicon(url: url, blacklistedExtensions: [".svg"])
let smallIconUrl = await favicon.url(size: .small)
let mediumIconUrl = await favicon.url(size: .medium)
let largeIconUrl = await favicon.url(size: .large)
Returned result is the highest quality icon within it's class.
Unless the size is set explicitly (through sizes
attribute) size class is just an educated guess e.g. rel="icon"
, "shortcut icon" etc. are small, "apple-touch" stuff is medium, the rest is large.
If size is set then everything below 32 pixels is small, up to 180 is medium and the rest is large.
This seems to give pretty good results but your mileage may vary. Feel free to open an issue if it doesn't work for you.
If you're unhappy with how this library sorts and groups icons you can request all icons that this library could find and pick what you want:
let icons: [Icon] = await favicon.icons()
struct Icon {
let url: URL // Absolute url to the icon
let iconClass: IconClass // Enum. Icon source from "rel" attribute usually. .appleTouchIcon, .icon etc.
let sizeClass: IconSizeClass // Enum. .small, .medium or .large
}