Как указать конкретный день и время при программном добавлении события календаря - PullRequest
2 голосов
/ 01 апреля 2012

Я пытаюсь программно добавить событие в календарь по умолчанию, но не могу найти, как я устанавливаю время, в которое происходит событие, и продолжительность события.Все, чего я достиг, выполнив ряд различных уроков, - это создание события на весь день, которое повторяется каждый день.Код, который у меня есть, приведен ниже.

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];

Ответы [ 2 ]

0 голосов
/ 21 апреля 2012

Ошибка, которую я сделал, заключалась не в том, как я думал о классах и названиях классов.Кажется глупо, я знаю.Все сделано с начальной и конечной датой, и я ожидаю увидеть больше атрибутов, чтобы установить точное время.Но теперь я вижу, что это делается путем получения нужного количества секунд и использования NSDate.

0 голосов
/ 13 апреля 2012

Следовали ли вы этой документации ?? http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html

Вы видели пример кода яблока ?? http://developer.apple.com/library/ios/#samplecode/SimpleEKDemo/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010160

В этой строке вы можете установить время event.startDate = ваше время с объектом даты.

...