改变APP的图标

下面介绍的应用图标切换的方法只是其中的一个思路.并不是说要切换应用图标只有这一种方法.如果有其他更好的方法可以私信交流一下.

1.首先,这个方法需要设置Info.plist文件.

这里写图片描述

字段名1:CFBundleAlternateIcons
字段名2:CFBundleIconFiles

///这是Source Code版的
<key>CFBundleIcons</key>
	<dict>
		<key>CFBundleAlternateIcons</key>
		<dict>
			<key>AppIconBlue</key>
			<dict>
				<key>CFBundleIconFiles</key>
				<array>
					<string>appIconBlueiPhoneApp_60pt</string>
				</array>
			</dict>
			<key>AppIconRed</key>
			<dict>
				<key>CFBundleIconFiles</key>
				<array>
					<string>appIconRediPhoneApp_60pt</string>
				</array>
			</dict>
		</dict>
		<key>CFBundlePrimaryIcon</key>
		<dict>
			<key>CFBundleIconFiles</key>
			<array>
				<string></string>
			</array>
			<key>UIPrerenderedIcon</key>
			<false/>
		</dict>
	</dict>

item0是String类型的,item0的内容就是图片的名字.可以不带图片的拓展名.

	///切换应用图标
    @IBAction func whiteAppIconBtnClick(_ sender: UIButton) {
        if supportAlternateIcons() == true {
            UIApplication.shared.setAlternateIconName(nil, completionHandler:nil)
        }
    }

    @IBAction func redAppIconBtnClick(_ sender: UIButton) {
        if supportAlternateIcons() == true {
            UIApplication.shared.setAlternateIconName("AppIconRed", completionHandler:nil)
        }
    }

    @IBAction func blueAppIconBtnClick(_ sender: UIButton) {
        if supportAlternateIcons() == true {
            UIApplication.shared.setAlternateIconName("AppIconBlue", completionHandler:nil)
        }
    }

    func supportAlternateIcons() -> Bool {///判断是否支持切换应用的APPIcon
        return UIApplication.shared.supportsAlternateIcons
    }

演示

Swift中的方法交换

我们看到.每次切换应用图片的时候都会有一个弹窗,我们现在想要把这个弹窗给去掉.那么怎么去掉呢.
按照OC的Runtime.那么就是MethodSwizzing.也就是方法交换啦.

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        ///交换方法去使系统的APP图标已经改变不弹出
        let firstMethod = class_getInstanceMethod(self.classForCoder,#selector(dy_present(_:animated:completion:)))
        let secondMethod = class_getInstanceMethod(self.classForCoder,#selector(present(_:animated:completion:)))
        method_exchangeImplementations(firstMethod!, secondMethod!)
    }

	func dy_present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if viewControllerToPresent.isKind(of: UIAlertController.classForCoder()) {
            print("yes")
        }
    }

但是按照OC的这个来写的话.方法并不会交换.
原因,OC是消息机制的一门语言.调用什么方法是在程序运行的时候才决定的.就相当于.方法A,B,C,D都在一个小黑屋里面.对象AA往小黑屋里面喊话.我要调用A方法啦.一般情况下,A方法就被调用了.方法交换是个什么意思呢.就相当于喊A的时候,这时候小黑屋的管理员说.A你不用出去执行方法.你和C交换一下.差不多是这个意思.

解决:在要交换的方法处插入@objc dynamic

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        ///交换方法去使系统的APP图标已经改变不弹出
        let firstMethod = class_getInstanceMethod(self.classForCoder,#selector(dy_present(_:animated:completion:)))
        let secondMethod = class_getInstanceMethod(self.classForCoder,#selector(present(_:animated:completion:)))
        method_exchangeImplementations(firstMethod!, secondMethod!)
    }

    @objc dynamic func dy_present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if viewControllerToPresent.isKind(of: UIAlertController.classForCoder()) {
            print("yes")
        }
    }

运行结果:
这里写图片描述

github地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值