美文网首页
日历提醒

日历提醒

作者: CAICAI0 | 来源:发表于2018-04-27 11:29 被阅读15次

    直接上关键函数

    - (void)saveEventStartDate:(NSDate*)startDate
                       endDate:(NSDate*)endDate
                         alarm:(float)alarm
                    eventTitle:(NSString*)eventTitle
                      location:(NSString*)location
                    isReminder:(BOOL)isReminder
    {
        //写⼊入事件
        if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
        {
            __weak typeof(self)WeakSelf = self;
            [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
             {
                 dispatch_async(dispatch_get_main_queue(), ^
                                {
                                    if (error)
                                    {
                                        NSLog(@"%@",error.localizedDescription);
                                    }
                                    else
                                        if (!granted)
                                        {
                                            //被⽤户拒绝,不允许访问日历
                                            NSLog(@"用户不允许访问日历");
                                        }
                                        else
                                        {
                                            //是否写入提醒事项,提醒事项为iOS原生自带的,但是模拟器没有
                                            if (isReminder)
                                            {
                                                EKCalendar * iDefaultCalendar = [eventStore defaultCalendarForNewReminders];
                                                EKReminder *reminder=[EKReminder reminderWithEventStore:eventStore];
                                                reminder.calendar=[eventStore defaultCalendarForNewReminders];
                                                reminder.title=eventTitle;
                                                reminder.calendar = iDefaultCalendar;
                                                EKAlarm *alarm=[EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:-10]];
                                                [reminder addAlarm:alarm];
                                                
                                                NSError *error=nil;
                                                [eventStore saveReminder:reminder commit:YES error:&error];
                                                if (!error)
                                                {
                                                    NSLog(@"error=%@",error.localizedDescription);
                                                }
                                            }
                                            else
                                            {
                                                if(startDate && endDate)
                                                {
                                                    [WeakSelf deleteInsertedEvent];
                                                    //根据开始时间和结束时间创建谓词
                                                    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
                                                    if(predicate)
                                                    {
                                                        //根据谓词条件筛选已插入日历的事件
                                                        NSArray *eventsArray = [eventStore eventsMatchingPredicate:predicate];
                                                        if (eventsArray.count)
                                                        {
                                                            for (EKEvent *item in eventsArray)
                                                            {
                                                                //根据事件的某个唯一性,如果已插入日历就不再插入
                                                                if([item.title isEqualToString:eventTitle])
                                                                {
                                                                    return ;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                //创建事件
                                                EKEvent *event = [EKEvent eventWithEventStore:eventStore];
                                                //主标题
                                                event.title = eventTitle;
                                                //副标题
                                                event.location = location;
                                                //事件设定为全天事件
                                                event.allDay = NO;
                                                //设定事件开始时间
                                                event.startDate=startDate;
                                                //设定事件结束时间
                                                event.endDate=endDate;
                                                //设定URL,点击可打开对应的app:AAA为某应用对外访问的入口
                                                event.URL = [NSURL URLWithString:@"AAA://https://www.baidu.com"];
                                                //添加提醒,可以添加多个
                                                //设定事件在开始时间多久前开始提醒
                                                [event addAlarm:[EKAlarm alarmWithRelativeOffset:alarm]];
                                                event.calendar = [eventStore defaultCalendarForNewEvents];
                                                NSError *error;
                                                [eventStore saveEvent:event span:EKSpanThisEvent error:&error];
                                                if (!error)
                                                {
                                                    UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:@"提示"
                                                                         message:@"设置提醒成功!"
                                                              preferredStyle:UIAlertControllerStyleAlert];
                                                    [alertvc addAction:[UIAlertAction
                                                       actionWithTitle:@"确定"
                                                                 style:UIAlertActionStyleCancel
                                                               handler:nil]];
                                                    [self presentViewController:alertvc animated:YES completion:nil];
                                                }
                                            }
                                        }
                                });
             }];
        }
    }
    
    998F4317EDE94AA9E844168969A549AF.jpg 534F031FD30A1605F5C63D2CA98AD0FC.jpg DB70537A-7FE9-4C31-9A6E-DEA6741264FA.png

    相关文章

      网友评论

          本文标题:日历提醒

          本文链接:https://www.haomeiwen.com/subject/wpdblftx.html