
iOS编程(34)

版权声明:本文为博主原创文章,未经博主允许不得转载。
1.首先我们初始化一个 UISwith
- self.swith = [[UISwitch alloc] initWithFrame:CGRectMake(80, 80, 160, 30)];
- [_swith addTarget:self action:@selector(doLocalNotifition) forControlEvents:UIControlEventValueChanged];
- [_swith setOn:NO];
- [self.view addSubview:_swith];
2.实现UISwith的方法
- - (void)doLocalNotifition
- {
- if (_swith.isOn==YES) {
- //初始化一个 UILocalNotification
- UILocalNotification * notification = [[UILocalNotification alloc] init];
- NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
- if (notification!=nil) {
- //设置 推送时间
- notification.fireDate= pushDate;
- //设置 时区
- notification.timeZone = [NSTimeZone defaultTimeZone];
- //设置 重复间隔
- notification.repeatInterval = kCFCalendarUnitDay;
- //设置 推送 时间
- notification.soundName = UILocalNotificationDefaultSoundName;
- //设置 推送提示语
- notification.alertBody = @"提示框内容5";
- //设置 icon 上 红色数字
- notification.applicationIconBadgeNumber = 1;
- //取消 推送 用的 字典 便于识别
- NSDictionary * inforDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- notification.userInfo =inforDic;
- //添加推送到 Application
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
- }
- NSLog(@"开启本地通知");
- }else if(_swith.isOn==NO){
- //拿到 存有 所有 推送的数组
- NSArray * array = [[UIApplication sharedApplication] scheduledLocalNotifications];
- //便利这个数组 根据 key 拿到我们想要的 UILocalNotification
- for (UILocalNotification * loc in array) {
- if ([[loc.userInfo objectForKey:@"key"] isEqualToString:@"name"]) {
- //取消 本地推送
- [[UIApplication sharedApplication] cancelLocalNotification:loc];
- }
- }
- NSLog(@"关闭本地通知");
- }
- }