2012-04-01 43 views
2

我試圖以編程方式將事件添加到默認日曆中,但無法找到我如何設置事件發生的時間以及事件的持續時間。通過遵循許多不同的教程,我迄今爲止完成的所有工作都是創建一個每天都會重複出現的全天事件。我到目前爲止的代碼如下。如何指定以編程方式添加日曆事件的特定日期和時間

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

仍然沒有得到哪一行是日期問題 – NSCry 2012-04-13 09:59:13

+0

這是我從教程中使用的代碼組合。我不知道如何讓事件發生在特定的日子或時間。這種情況每天都會發生。 – dmeads89 2012-04-13 10:02:22

回答

0
+0

我遵循了文檔,並且找不到說明如何將其設置爲特定時間的任何地方。文檔中的重現規則部分似乎最相關,但我仍然無法弄清楚時間方面。 蘋果在該鏈接中的示例代碼或從日曆中檢索事件我認爲? 道歉我缺乏知道如何通常在使用文檔,但這次我很難過。 – dmeads89 2012-04-13 10:21:30

0

我所犯的錯誤並不是我在想如何使用類和類名。似乎我知道愚蠢。一切都是以開始和結束日期完成的,我希望看到更多的屬性來設定確切的時間。但是現在我看到這是通過獲得適當的秒數和使用NSDate完成的。

相關問題