How to get the current application icon in ios How to get the current application icon in ios ios ios

How to get the current application icon in ios

Works for Swift 4.1 and extending it for Bundle.

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    }}

To use in an app, call Bundle.main.icon.

Here is a Swift 4.x && 3.x extension to UIApplication for obtaining the application icon. You can choose whether to get the smallest or largest icon based on the location of the icon path you pull from the iconFiles array.

extension UIApplication {    var icon: UIImage? {        guard let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? NSDictionary,            let primaryIconsDictionary = iconsDictionary["CFBundlePrimaryIcon"] as? NSDictionary,            let iconFiles = primaryIconsDictionary["CFBundleIconFiles"] as? NSArray,            // First will be smallest for the device class, last will be the largest for device class            let lastIcon = iconFiles.lastObject as? String,            let icon = UIImage(named: lastIcon) else {                return nil        }        return icon    }}

To access the icon, call the following:

let icon = UIApplication.shared.icon

For bonus points, you could even make two vars to get the smallest and largest icon if your app needed it.

The accepted answer did not work for me, I am using Xcode 5's Images.xcassets method of storing app icons. This modification worked for me:

UIImage *appIcon = [UIImage imageNamed: [[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIcons"] objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"]  objectAtIndex:0]];

When in doubt, just explore the main bundle's infoDictionary using lldb.