-1

I have this piece of code ...

guard let filePath = Bundle.main.path(forResource: "AppIcon", ofType: "png") else {
        print("Image not found")
        return nil
    }

This is returning nil, which I can't seem to figure out. I assume my file path is wrong. Any suggestions?

c0nman
  • 189
  • 1
  • 12
  • 3
    Looks like you are trying to access a folder `AppIcon`, with the extension `png`? Try changing the `ofType` to `.appiconset`. – George Dec 16 '19 at 00:28
  • 2
    Hi check [this](https://stackoverflow.com/questions/9419261/how-to-get-the-current-application-icon-in-ios) answer, could help you – Sailendra Dec 16 '19 at 03:15

1 Answers1

2

Try this. I hope this may be helpful to you.

extension Bundle {
 public var icon: UIImage? {
    if let icons = 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
  }
}

You can use it in your app like this:

let imageView = UIImageView()
imageView.image = Bundle.main.icon
Abhishek Patel
  • 121
  • 1
  • 1
  • 11
  • 1
    I'm using let attachement = try? UNNotificationAttachment(identifier: "attachment", url: fileURL, options: nil), so i need the url. I tried the extension with iconFiles.last, but still couldn't get it. It's returning nil. Just can't figue it out. – c0nman Dec 20 '19 at 18:25