6

This is an update to this outdates question: Alternate Icon in iOS 10.3: avoid notification dialog for icon change


func setAppIcon(Type: String) {
    if #available(iOS 10.3, *) {
       UIApplication.shared.setAlternateIconName(Type)
    }
}

With the few lines above it is possible to change the Appicon dynamically, the feature was added with iOS 10.3.

The code above is working fine but every time the app icon changed iOS triggers an alert like this:

img

So is there a way to get rid of this alert? (I know that apple could reject application for disabling user-information but I'd like to use it just for test purposes)

Any help would be SUPER appreciated, thanks! :-)

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

5

Try the following code, however, it is written with Objective-c. It makes use of private method, I guess that you will never mind.

- (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
  • Great.. will they reject the app? – Nithin Michael Jun 26 '19 at 11:43
  • 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
  • How do you convert the `void (*func)(id, SEL, id, id) = (void *)imp; if (func) { func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {}); }` to swift? – Zonily Jame Jul 30 '19 at 10:33
  • Solution worked great for me, wasn't able to convert the final bit too swift. but used a good old fashion bridge file and worked fine – MindBlower3 Nov 21 '19 at 11:17
  • Here my swift version. Problem is, that the alert still shows :/ Maybe I made a mistake and someone has a better idea: `let selector = #selector(UIApplication.shared.setAlternateIconName) let imp = UIApplication.shared.method(for: selector) typealias ClosureType = @convention(c) (AnyObject, Selector, String?, ((Error?) -> Void)? ) -> Void let curry : ClosureType = unsafeBitCast(imp, to: ClosureType.self) curry(UIApplication.shared, selector, iconName, nil)` – themenace Jul 19 '20 at 09:18
  • If they catch you using a private api you get a warning, get caught more than once you run a risk of getting banned. – James Wolfe Mar 30 '21 at 20:13