iOS 动态更换icon

此方法只适用于iOS10.3

// Pass nil to use the primary application icon. The completion handler will be invoked asynchronously on an arbitrary background queue; be sure to dispatch back to the main queue before doing any further UI work.

  • (void)setAlternateIconName:(nullable NSString *)alternateIconName completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler NS_EXTENSION_UNAVAILABLE("Extensions may not have alternate icons") API_AVAILABLE(ios(10.3), tvos(10.2));

在info.plist中添加下面信息

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundleAlternateIcons</key>
        <dict>
            <key>star</key>
            <dict>
                <key>CFBundleIconFiles</key>
                <array>
                    <string>star-29</string>
                    <string>star-20</string>
                    <string>star-40</string>
                    <string>star-60</string>
                </array>
                <key>UIPrerenderedIcon</key>
                <true/>
            </dict>
        </dict>
    </dict>

info.png

主要实现代码

-(void)setIcon:(NSString *)iconName
{
//默认的icon,iconName =nil;
    NSString *appiconName = [[UIApplication sharedApplication] alternateIconName];
    NSLog(@"目前iconName %@",appiconName);
    
    [[UIApplication sharedApplication]setAlternateIconName:iconName completionHandler:^(NSError * error){
        NSLog(@"samli error %@",error);
    }];
}

代码链接
END

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

推荐阅读更多精彩内容