iOS 无弹框换图标,直接用就可以了

Demo:更换图标demo,如有问题,请随时联系  

需求:换图标,用户无感

1.分析:

        对于iOS,是开发了API可以换图标,但是有规定的,就是你要换的图标是要在APP里面内置的,也就是说你要换的APPicon,是提前放在APP包里面的,具体后面会有解释,好吧直接上代码吧。

2.代码实现过程

    (1)Info.plist配置,直接添加(以下代码直接复制到info.plist中):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleAlternateIcons</key>
	<dict>
		<key>MayOneIcon</key>
		<dict>
			<key>CFBundleIconFiles</key>
			<array>
				<string>MayOneIcon</string>
			</array>
			<key>UIPrerenderedIcon</key>
			<false/>
		</dict>
	</dict>
	<key>UINewsstandIcon</key>
	<dict>
		<key>CFBundleIconFiles</key>
		<array>
			<string></string>
		</array>
		<key>UINewsstandBindingEdge</key>
		<string>UINewsstandBindingEdgeLeft</string>
		<key>UINewsstandBindingType</key>
		<string>UINewsstandBindingTypeMagazine</string>
	</dict>
</dict>
</plist>

    (2)导入预制的图标,120 * 120的就行了,然后名字要和plist中里面的对应,我这里用的是MayOneIcon;(注)这个名字在plist中里面有俩个地方需要修改。

     (3)在需要调用改图标的地方写入换图标的代码:

/*  直接调用此方法,传入数据为iconName,也就是后台给你要换的图标
 默认传的是@"DefaultIcon"
 */
- (void)chengeAppicon:(NSString*)iconNameNew{
    if (iconNameNew.length == 0) {
        return;
    }
    if ([UIApplication sharedApplication].supportsAlternateIcons) {
        NSLog(@"you canExchange");
    }else{
        NSLog(@"you can not Exchange");
        return;
    }
    
    NSString * iconname = [[UIApplication sharedApplication]alternateIconName];
    if ((!iconname&&[iconNameNew isEqualToString:@"DefaultIcon"]) || [iconname isEqualToString:iconNameNew]) {
        return;
    }
    [self exchangealterMethod];
    if ( [iconNameNew isEqualToString:@"DefaultIcon"]) {
        
        [[UIApplication sharedApplication]setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"error");
            }else
                NSLog(@"icon name:%@",iconname);
            
        }];
    }else{
        [[UIApplication sharedApplication]setAlternateIconName:iconNameNew completionHandler:^(NSError * _Nullable error) {
            
            if (error) {
                NSLog(@"error");
            }else
                NSLog(@"icon name:%@",iconname);
            
        }];
        
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self exchangealterMethod];
    });
}

- (void)dy_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {
        NSLog(@"title : %@",((UIAlertController *)viewControllerToPresent).title);
        NSLog(@"message : %@",((UIAlertController *)viewControllerToPresent).message);
        
        UIAlertController *alertController = (UIAlertController *)viewControllerToPresent;
        if (alertController.title == nil && alertController.message == nil) {
            
            return;
        } else {
            [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
            return;
        }
    }
    
    [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
- (void)exchangealterMethod{
    Method presentM = class_getInstanceMethod(UIViewController.class, @selector(presentViewController:animated:completion:));
    Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:));
    
    method_exchangeImplementations(presentM, presentSwizzlingM);
}







猜你喜欢

转载自blog.csdn.net/mayxc/article/details/79755105