我試圖以編程方式將事件添加到默認日曆中,但無法找到我如何設置事件發生的時間以及事件的持續時間。通過遵循許多不同的教程,我迄今爲止完成的所有工作都是創建一個每天都會重複出現的全天事件。我到目前爲止的代碼如下。如何指定以編程方式添加日曆事件的特定日期和時間
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
// use this in the method that actually creates the event
NSError *err = nil;
//This date will be the date our reminder expires, as in stops recurring. Two years was chosen as most users
//will replace their device after 2 years
NSDate *fourteenWeeksFromNow = [NSDate dateWithTimeIntervalSinceNow:8467200];
//Define the recurrance rule
EKRecurrenceRule *recurrance;
NSDateComponents *comp = [[NSDateComponents alloc]init];
[comp setYear:0];
[comp setMonth:0];
[comp setDay:7];
//Recurr every 7 days I think but not working
recurrance = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyWeekly interval:1 end:[EKRecurrenceEnd recurrenceEndWithEndDate:fourteenWeeksFromNow]];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//Our remind date
NSDate *eventDate = [cal dateByAddingComponents:comp toDate:[NSDate date] options:0]; // unsure what this does or how to use it
double alarmAmountInSeconds = 60.0*60.0*0.25; // 15 mins
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(-1.0*alarmAmountInSeconds)]; // this is working correctly, alerts 15 minutes before
NSMutableArray *alarmsArray = [[NSMutableArray alloc] init];
[alarmsArray addObject:alarm];
[recurrenceRules arrayByAddingObject:recurrance];
event.title = module;
event.startDate = [[NSDate alloc] init]; // works
event.endDate = [[NSDate alloc] initWithTimeInterval:8467200 sinceDate:event.startDate]; // works
event.calendar = eventStore.defaultCalendarForNewEvents; // works
event.title = module; // works
event.allDay = FALSE; // dont want it all day but need to be able find out how to set time
event.location = @"Test location";
event.alarms = alarmsArray; // works
event.recurrenceRules = recurrenceRules; // not working could be problems with other attributes though
event.notes = @"This is a test"; // works
// Try to save the event
[eventStore saveEvent:event span:EKSpanFutureEvents error:&err];
仍然沒有得到哪一行是日期問題 – NSCry 2012-04-13 09:59:13
這是我從教程中使用的代碼組合。我不知道如何讓事件發生在特定的日子或時間。這種情況每天都會發生。 – dmeads89 2012-04-13 10:02:22