1 #import "AppDelegate.h"
2
3 @interface AppDelegate ()
4
5 @end
6
7 @implementation AppDelegate
8
9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11 //注册推送
12 //申请设置角标数字权限
13 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]];
14
15 return YES;
16 }
17 //观察者回调方法
18 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
19 {
20 //获取badgeValue的新值
21 NSString *value = change[NSKeyValueChangeNewKey];
22 NSLog(@"%@",value);
23 [UIApplication sharedApplication].applicationIconBadgeNumber = value.integerValue ;
24 }
1 #import "ViewController.h"
2 #import "AppDelegate.h"
3 @interface ViewController ()
4
5 @end
6
7 @implementation ViewController
8
9 - (void)viewDidLoad {
10 [super viewDidLoad];
11
12
13 AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
14 //注册观察者
15 [self addObserver:appDelegate forKeyPath:@"tabBarItem.badgeValue" options:NSKeyValueObservingOptionNew context:nil];
16 //触发观察者
17 self.tabBarItem.badgeValue = @"9";
18 self.tabBarItem.badgeValue = @"123";
19 self.tabBarItem.badgeValue = @"456";
20 //移除观察者
21 [self removeObserver:appDelegate forKeyPath:@"tabBarItem.badgeValue"];
22
23 }
24
25 - (void)didReceiveMemoryWarning {
26 [super didReceiveMemoryWarning];
27 // Dispose of any resources that can be recreated.
28 }