Alternate Icon in iOS 10.3: avoid notification dialog for icon change
Asked Answered
T

5

20

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 ?

Typographer answered 11/4, 2017 at 21:34 Comment(1)
I don't think this is possible - this alert is shown by the system. Not displaying it could lead to potential abuse.Intervocalic
F
17

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) {});
        }
    }
}
Finfoot answered 9/4, 2018 at 9:53 Comment(4)
Great.. will they reject the app?Shewmaker
My apps aren't been rejected, so you can have a try. Variable selectorString is anti code static analysis.Finfoot
how do you convert this to Swift 5 @RobertDisclosure
Scanbot is doing it in their app, so I guess it should be fine to use for nowAircondition
L
17

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 })
        }
    }
Laguna answered 14/9, 2020 at 2:0 Comment(0)
H
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 })
        }
    }
Harvell answered 8/9, 2020 at 7:26 Comment(3)
I can confirm it works in iOS16, too. waiting to Store approval..Sedimentary
Did you get the approve?Harvell
approved, BUT seems not working in iPad.. we will investigate further.Sedimentary
P
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;
Paracelsus answered 27/1, 2023 at 18:30 Comment(0)
C
0
import UIKit

extension UIApplication {
    func setAppIcon(_ appIcon: String?) {
        typealias setAlternateIconName = @convention(c) (
            NSObject, Selector, NSString?, @escaping (NSError?) -> Void
        ) -> Void
        
        let selector = NSSelectorFromString(
            "_setAlternateIconName:completionHandler:"
        )

        if let imp = method(for: selector) {
            let method = unsafeBitCast(imp, to: setAlternateIconName.self)
            method(self, selector, appIcon as NSString?, { _ in })
        }
    }
}
Carbon answered 19/2 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.