iOS 動態更換icon

A訫飛Flyme發表於2017-12-20

此方法只適用於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

相關文章