iOS 如何更换app图标

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,824评论 0 9
  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,661评论 33 466
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 交通 小田急箱根フリーパス 价格 两日券新宿出发5140每人 购入方法 箱根フリーパスは、箱根エリアの主要な駅だと...
    maxon阅读 298评论 0 1
  • 最近忙的累翻天,从早上到中午吃饭基本上没有喝水上厕所的时间,就连中午换饭吃都吃的匆匆忙忙。更让我绝望的是近三个月的...
    想见的平行线81阅读 162评论 0 0