需求:根据后台给定名字切换App图标
解决方案:
在本地放置需要切换的一些图片,在plist文件里面加一些参数,然后再调用方法,方法如下:
- (void)setAppIconWithName:(NSString*)iconName {
if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
return;
}
if([iconNameisEqualToString:@""]) {
iconName =nil;
}
[[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
if(error) {
NSLog(@"更换app图标发生错误了 : %@",error);
}
}];
}
需要注意的是需求修改图标成功后不让弹出提示框,所以用runtime-swizzing交换方法,代码如下:
@implementationUIViewController (Present)
+ (void)load {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
MethodpresentM =class_getInstanceMethod(self.class,@selector(presentViewController:animated:completion:));
MethodpresentSwizzlingM =class_getInstanceMethod(self.class,@selector(ZB_presentViewController:animated:completion:));
// 交换方法
method_exchangeImplementations(presentM, presentSwizzlingM);
});
}
- (void)ZB_presentViewController:(UIViewController*)viewControllerToPresent animated:(BOOL)animation completion:(void(^)(void))completion {
//查看无弹框的打开这个注释
if([viewControllerToPresentisKindOfClass:[UIAlertControllerclass]]) {
UIAlertController*alertController = (UIAlertController*)viewControllerToPresent;
NSLog(@"==== title == %@", alertController.title);
NSLog(@"==== message == %@", alertController.message);
if(alertController.title==nil&& alertController.message==nil) {
return;
}
}[selfZB_presentViewController:viewControllerToPresentanimated:animationcompletion:completion];
}
具体参考链接:
https://juejin.im/post/5c04e4c06fb9a049aa6ed50d
感谢大佬分享!
网友评论