我正在开发一款日历应用。
有一个月我一整天都在玩这一天的活动。我必须这样做才能处理超过一天的复发事件和事件。
除了一个案例之外它很有效:一个为期数天的事件的最后一天。我看到这个事件的其他日子的事件,但不是最后一个事件。 (我在GMT + 1时区,这就是我有这个时间的原因)
SEARCH FOR THE LAST DAY OF EVENT
Start: 2013-03-25 23:00:00 +0000
End: 2013-03-26 22:59:59 +0000
EVENT
Start: 2013-03-24 21:00:06 +0000
End: 2013-03-26 21:00:06 +0000
No results!
以下是返回当天事件的方法:
+ (NSArray *)ekEventsWithStartDate:(NSDate*)startDate endDate:(NSDate*)endDate
{
NSLog(@"ekEventsWithStartDate:%@ endDate:%@",startDate,endDate);
NSPredicate *predicate = [_eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:nil];
NSArray *events = [_eventStore eventsMatchingPredicate:predicate];
NSLog(@"events (%d):%@",[events count],events);
return events;
}
以下是活动详情:
EKEvent <0xb0635e0> {EKEvent <0xb0635e0>
{title = 24-26 Mars 10 PM;
location = ;
calendar = EKCalendar <0xb3c3c80> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;};
alarms = (null);
URL = (null);
lastModified = 2013-03-19 22:11:10 +0000;
timeZone = Europe/Paris (GMT+01:00) offset 3600};
location = ;
startDate = 2013-03-24 21:00:06 +0000;
endDate = 2013-03-26 21:00:06 +0000;
allDay = 0;
floating = 0;
recurrence = (null);
attendees = (null)}
以下是来自ekEventsWithStartDate方法的3天此日志的日志:
ekEventsWithStartDate:2013-03-23 23:00:00 +0000 endDate:2013-03-24 22:59:59 +0000
events (1):(
"EKEvent <0x9b44c00> {EKEvent <0x9b44c00> {title = 24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)
ekEventsWithStartDate:2013-03-24 23:00:00 +0000 endDate:2013-03-25 22:59:59 +0000
events (1):(
"EKEvent <0xb28b970> {EKEvent <0xb28b970> {title = 24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)
ekEventsWithStartDate:2013-03-25 23:00:00 +0000 endDate:2013-03-26 22:59:59 +0000
events (0):(null)
为什么该方法返回null数组?
附属问题:是否有更好的方法来获取一个月中每一天的活动?我正在寻找更好的表现。
感谢您的帮助!
编辑20/03/2013: 我要感谢Dhruvik,我的代码适用于iOS 5.X但不适用于iOS 6.X(没有测试4.X)。
我检查5.X和6.X版本的事件和日期,我看到的唯一区别是事件日历时区属性:
iOS 5.X
timeZone = Europe/Paris (CET)
iOS 6.X
timeZone = Europe/Paris (UTC+01:00)
此问题与全天活动无关。
您对iOS 6.X有同样的问题吗?
答案 0 :(得分:3)
以下是获取事件的代码,我在我的应用中实现了这个代码..
in .h
@property (nonatomic, retain) EKEventStore *eventStore;
@property (nonatomic, retain) EKCalendar *defaultCalendar;
@property (nonatomic, retain) NSMutableArray *eventsList;
in .m
//This code will fecth the events from one day Before the current date..
- (void) fetchevents
{
eventStore = [[EKEventStore alloc] init];
eventsList = [[NSMutableArray alloc] init];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
}];
// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1; // From which date you have to fetch Events from calander, as per your need subtract the days from current date
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:[NSDate date]
options:0];
NSLog(@"%@", oneDayAgo);
// Create the end date components
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
oneYearFromNowComponents.year = 0;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
toDate:[NSDate date]
options:0];
NSLog(@"%@", oneYearFromNow);
//Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil];
NSArray *events_Array = [eventStore eventsMatchingPredicate: predicate];
for (EKEvent *eventToCheck in events_Array)
{
[eventsList addObject:eventToCheck.title ];
}
}
希望它有所帮助。,快乐编码..谢谢