【iOS】10.3以后动态在手机桌面图表管理
首先需要info.plist文件配置,这里test1 、test2是两组替换图标,配置文件里保持名称一致,图中87是程序本身的图标名称之一。
//////更换之前需要在inf.plist进行相应设置
-(void)iconType:(NSString *)type{
////该方法10.3以后的系统才支持:
if ([UIApplication instancesRespondToSelector:@selector(setAlternateIconName:completionHandler:)]) {
NSLog(@"you can change this app's icon");
}else{
NSLog(@"you can not change this app's icon");////该方法10.3以后的系统才支持
return;
}
if ([UIApplication sharedApplication].supportsAlternateIcons) {
NSLog(@"you can change this app's icon");
}else{
NSLog(@"you can not change this app's icon");
return;
}
///NSString *iconName = [[UIApplication sharedApplication] alternateIconName]; .supportsAlternateIcons
switch ([typeintValue]) {
case1:
[[UIApplicationsharedApplication] setAlternateIconName:nilcompletionHandler:^(NSError *_Nullable error) {
DLog(@"原始图标");
}];
break;
case2:
[[UIApplicationsharedApplication] setAlternateIconName:@"test1"completionHandler:^(NSError *_Nullable error) {
DLog(@"新换图标");
}];
break;
case3:
[[UIApplicationsharedApplication] setAlternateIconName:@"test2"completionHandler:^(NSError *_Nullable error) {
DLog(@"新换图标");
}];
break;
default:
break;
}
}
以上配置后调用上面方法即可!注:名称保持一致
#########################################################################################################################################
以上方法会有系统弹窗,下面是运行时替换弹窗方法:
#import <objc/runtime.h>
................./////在当前viewController下执行以下两个方法 替换更换图标的弹窗
///// 利用runtime来替换展现弹出框的方法
- (void)runtimeReplaceAlert:(UIViewController *)controller
{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method presentM =class_getInstanceMethod(controller.class,@selector(presentViewController:animated:completion:));
Method presentSwizzlingM =class_getInstanceMethod(self.class,@selector(ox_presentViewController:animated:completion:));
// 交换方法实现
method_exchangeImplementations(presentM, presentSwizzlingM);
});
}
// 自己的替换展示弹出框的方法
- (void)ox_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if ([viewControllerToPresentisKindOfClass:[UIAlertControllerclass]]) {
NSLog(@"title : %@",((UIAlertController *)viewControllerToPresent).title);
NSLog(@"message : %@",((UIAlertController *)viewControllerToPresent).message);
// 换图标时的提示框的title和message都是nil,由此可特殊处理
UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
if (alertController.title ==nil && alertController.message ==nil) { //是换图标的提示
return;
} else {//其他提示还是正常处理
[selfox_presentViewController:viewControllerToPresentanimated:flag completion:completion];
return;
}
}
[selfox_presentViewController:viewControllerToPresentanimated:flag completion:completion];
}
下图是调用方法:
封装文件下载:
文件地址已上传GitHub!!!!!!
第一次写博客,第一次共享代码,请多指教!!!!!!!!!!!!!!!!
应用点来自这里 小主只是修改成自己使用,记录一下