I'm using this new feature and when the change is made with an active application, a notification dialog is displayed saying that icon has been modified. How to disable this dialog ?
Asked
Active
Viewed 4,828 times
4 Answers
14
If you don't mind making use of private method, you can try the following code.
- (void)lc_setAlternateIconName:(NSString*)iconName
{
//anti apple private method call analyse
if ([[UIApplication sharedApplication] respondsToSelector:@selector(supportsAlternateIcons)] &&
[[UIApplication sharedApplication] supportsAlternateIcons])
{
NSMutableString *selectorString = [[NSMutableString alloc] initWithCapacity:40];
[selectorString appendString:@"_setAlternate"];
[selectorString appendString:@"IconName:"];
[selectorString appendString:@"completionHandler:"];
SEL selector = NSSelectorFromString(selectorString);
IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
void (*func)(id, SEL, id, id) = (void *)imp;
if (func)
{
func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {});
}
}
}

Robert
- 470
- 4
- 12
-
-
My apps aren't been rejected, so you can have a try. Variable selectorString is anti code static analysis. – Robert Jul 02 '19 at 08:56
-
3
-
Scanbot is doing it in their app, so I guess it should be fine to use for now – henrik-dmg Jun 05 '20 at 08:07
12
Adding to Andrew's Swift 5 rewrite of Robert's answer (as I don't have the reputation to comment).
For the default icon we need to pass nil, so iconName should be an optional.
func setApplicationIconName(_ iconName: String?) {
if UIApplication.shared.responds(to: #selector(getter: UIApplication.supportsAlternateIcons)) && UIApplication.shared.supportsAlternateIcons {
typealias setAlternateIconName = @convention(c) (NSObject, Selector, NSString?, @escaping (NSError) -> ()) -> ()
let selectorString = "_setAlternateIconName:completionHandler:"
let selector = NSSelectorFromString(selectorString)
let imp = UIApplication.shared.method(for: selector)
let method = unsafeBitCast(imp, to: setAlternateIconName.self)
method(UIApplication.shared, selector, iconName as NSString?, { _ in })
}
}

K Pajala
- 129
- 2
- 4
7
Rewrote Robert's answer in Swift. The app hasn't been rejected as well.
func setApplicationIconName(_ iconName: String) {
if UIApplication.shared.responds(to: #selector(getter: UIApplication.supportsAlternateIcons)) && UIApplication.shared.supportsAlternateIcons {
typealias setAlternateIconName = @convention(c) (NSObject, Selector, NSString, @escaping (NSError) -> ()) -> ()
let selectorString = "_setAlternateIconName:completionHandler:"
let selector = NSSelectorFromString(selectorString)
let imp = UIApplication.shared.method(for: selector)
let method = unsafeBitCast(imp, to: setAlternateIconName.self)
method(UIApplication.shared, selector, iconName as NSString, { _ in })
}
}

Andrew
- 344
- 3
- 10
-
-
-
approved, BUT seems not working in iPad.. we will investigate further. – ingconti Jul 13 '23 at 14:09
1
From the above Answer from Robert
- (void)lc_setAlternateIconName:(NSString*)iconName
{
//anti apple private method call analyse
if ([[UIApplication sharedApplication] respondsToSelector:@selector(supportsAlternateIcons)] &&
[[UIApplication sharedApplication] supportsAlternateIcons])
{
NSMutableString *selectorString = [[NSMutableString alloc] initWithCapacity:40];
[selectorString appendString:@"_setAlternate"];
[selectorString appendString:@"IconName:"];
[selectorString appendString:@"completionHandler:"];
SEL selector = NSSelectorFromString(selectorString);
IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
void (*func)(id, SEL, id, id) = (void *)imp;
if (func)
{
func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {});
}
}
}
I couldn't make the line
void (*func)(id, SEL, id, id) = (void *)imp;
For me it had to be
void (*func)(id, SEL, id, id) = (void (*)(id, SEL, id, id))imp;

Arthur Sady Cordeiro Rossetti
- 361
- 4
- 6