Is there a way to get the current application icon in a cocoa-touch app? Thank you.
This is how the icons is set inside the info.plist,
//try doing NSLog(@"%@",[[NSBundle mainBundle] infoDictionary]);
CFBundleIcons = {
CFBundlePrimaryIcon = {
CFBundleIconFiles = (
"icon.png",//App icon added by you
"icon@2x.png"//App icon in retina added by you
);
UIPrerenderedIcon = 1;
};
};
If you want to get the app icon use below code:
UIImage *appIcon = [UIImage imageNamed: [[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIcons"] objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
P.S: UIImage automatically picks the suitable icon for retina display.
I hope it helps!!
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
.
I based my answer completely on moosgummi's, but returning a UIImage
instead.
UIImage *appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
The UIImage
automatically selects the suitable version (normal, @2x
, ~ipad
, ~ipad@2x
or any future suffix). (BTW, don't you just hate this stupid scheme for identifying versions of images? It was just getting out of hand. Thank god for Asset Catalogues!)