iOS清除Icon红色角标
2024-01-02 本文已影响0人
BiBiMan
清除手机APP应用图标右上角的红色角标:
1.在你想要清除的地方执行以下代码
if (@available(iOS 16.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:nil];
}else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
2.在AppDelegate代理方法中执行以下代码(例如:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions{}
)
if (@available(iOS 16.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:nil];
}else {
[application setApplicationIconBadgeNumber:0];
}
API说明:
1.UNUserNotificationCenter实例方法只有16.0及以上系统版本可用
- (void)setBadgeCount:(NSInteger)newBadgeCount withCompletionHandler:(nullable void(^)(NSError *__nullable error))completionHandler
API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0)) API_UNAVAILABLE(watchos)
;
2.setter属性变量自17.0系统版本开始废弃
@property(nonatomic) NSInteger applicationIconBadgeNumber
API_DEPRECATED("Use -[UNUserNotificationCenter setBadgeCount:withCompletionHandler:] instead.", ios(2.0, 17.0))
;