美文网首页
iOS 如何更换app图标

iOS 如何更换app图标

作者: IWorld看看外面的精彩 | 来源:发表于2018-01-20 21:57 被阅读1226次

iOS10.3之后,苹果公开了“app-icon 换脸”的API(谁知道真正什么时候开放的,反正能用了)。先上成品:


123.gif

具体方法:
1.配置 info.plist 文件

    <key>CFBundleIcons</key>
    <dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string></string>
            </array>
        </dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>g2</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>g2</string>
                </array>
            </dict>
            <key>g3</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>g3</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <false/>
            </dict>
        </dict>
    </dict>
WechatIMG299.png
2.备用icon的名字自己取,图片放在项目目录下(黄文件夹) WechatIMG301.jpeg

3.代码实现

- (void)setIconname:(NSString *)name {
    UIApplication *appli = [UIApplication sharedApplication];
    //判断系统是否支持切换icon
    if ([appli supportsAlternateIcons]) {
        //切换icon
        [appli setAlternateIconName:name completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"error==> %@",error.localizedDescription);
            }else{
                NSLog(@"done!!!");
            }
        }];
    }
}

切回默认icon,只需将 name 为 nil 即可
到此成果为:

321.gif
这时你会看到出现提示,太不美观了,怎样去掉提示呢?
ps:http://www.cocoachina.com/ios/20170619/19557.html
这位大神利用Method swizzling hook(拦截)该弹框,什么是Method swizzling ?自己百度,我还是默默奉上链接(http://www.cocoachina.com/ios/20160826/17422.html),简而言之就是:给系统类写个分类,用自己的方法去替换系统方法的功能来达到自己想要的效果。
此项目中,通过拦截alertviewcontroller相关方法来实现去掉弹窗。
#import "UIViewController+present.h"
#import <objc/runtime.h>

@implementation UIViewController (present)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:));
        Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:));
        // 交换方法实现
        method_exchangeImplementations(presentM, presentSwizzlingM);
    });
}
- (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];
}
@end

ps:需要导入<objc/runtime.h>,不然。。。会报错的

Declaration of 'Method' must be imported from module 'ObjectiveC.runtime' before it is required

Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的优势在于:我们写代码时能够更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一个方法的实现等。这种特性意味着objective-c不仅需要一个编译器,还需要一个运行时系统来执行编译的代码。对于Objective-C来说,这个运行时系统就像一个操作系统一样:它让所有的工作可以正常的运行。这个运行时系统即Objc Runtime。Objc Runtime其实是一个Runtime库,它基本上是用C和汇编写的,这个库使得C语言有了面向对象的能力。runtime的强大之处在于它能在运行时创建类和对象。

123.gif

好了,结果就是那结果,完美实现,用什么问题请留言,我会认真改进的!!!

相关文章

网友评论

      本文标题:iOS 如何更换app图标

      本文链接:https://www.haomeiwen.com/subject/lmztaxtx.html