首页 > 其他 > 详细

日历相关EKEvent的学习记录

时间:2014-03-29 02:57:55      阅读:691      评论:0      收藏:0      [点我收藏+]

判断是否可以访问日历

EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted == YES) {
            NSLog(@"同意");
        }else{
            NSLog(@"不同意");
        }
    }];

获取日历事件


// 获取适当的日期(Get the appropriate calendar)
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 创建起始日期组件(Create the start date components)
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    oneDayAgoComponents.day = -1;
    NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:[NSDate date]
                                                 options:0];
    
    // 创建结束日期组件(Create the end date components)
    NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
    oneYearFromNowComponents.year = 1;
    NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                       toDate:[NSDate date]
                                                      options:0];
    
    // 用事件库的实例方法创建谓词 (Create the predicate from the event store‘s instance method)
    NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                            endDate:oneYearFromNow
                                                          calendars:nil];
    
    // 获取所有匹配该谓词的事件(Fetch all events that match the predicate)
    NSArray *events = [store eventsMatchingPredicate:predicate];
    for (EKEvent *e in events) {
        NSLog(@"%@",e.title);
        NSLog(@"%@",e.startDate);
        NSLog(@"%@",e.endDate);
        NSLog(@"-------------------------");
    }

添加一个日历事件


 // Get the event store object
    self.eventStore = [[EKEventStore alloc] init];
    
    // Create a new event
    EKEvent *event  = [EKEvent eventWithEventStore:self.eventStore];
    
    // Create NSDates to hold the start and end date
    NSDate *startDate = [[NSDate alloc] init];
    NSDate *endDate  = [[NSDate alloc] init];
    
    // Set properties of the new event object
    event.title     = @"这是测试";
    event.startDate =  [NSDate dateWithTimeIntervalSinceNow:-60*60];
    event.endDate   =  [NSDate dateWithTimeIntervalSinceNow:-30];
    event.allDay = NO;
    
    // set event‘s calendar to the default calendar
    [event setCalendar:[self.eventStore defaultCalendarForNewEvents]];
    
    // Create an NSError pointer
    NSError *err;

    // Save the event
    [self.eventStore saveEvent:event span:EKSpanThisEvent error:&err];
    
    self.idf = [NSString stringWithFormat:@"%@",event.eventIdentifier];
    NSLog(@"%@",event.eventIdentifier);
    
    // Test for errors
    if (err == noErr) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Event Created"
                              message:@"How about that?"
                              delegate:nil
                              cancelButtonTitle:@"Okay"
                              otherButtonTitles:nil];
        [alert show];  
        
    }  

删除一个日历事件

EKEvent *eventToRemove = [self.eventStore eventWithIdentifier:self.idf];
    
    if ([eventToRemove.eventIdentifier length] > 0) {
        NSError* error = nil;
        [self.eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
        if (error) {
            NSLog(@"%@",error);
        }else {
            NSLog(@"ok");
        }
    }

修改一个日历事件

EKEvent *eventToRemove = [self.eventStore eventWithIdentifier:self.idf];
    eventToRemove.title = @"已经修改";
    // Save the event
    [self.eventStore saveEvent:eventToRemove span:EKSpanThisEvent error:nil];


参考文章:

http://www.16kan.com/question/detail/265397.html

http://blog.csdn.net/linqir/article/details/8588835

http://supershll.blog.163.com/blog/static/3707043620127284210222/

日历相关EKEvent的学习记录,布布扣,bubuko.com

日历相关EKEvent的学习记录

原文:http://blog.csdn.net/ioswyl88219/article/details/22406451

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!