在IOS中创建事件时,日历事件正在多次添加

时间:2015-03-18 09:32:00

标签: ios objective-c ekevent

我在活动开始前5分钟创建了一个带闹钟的日历活动。

我的活动看起来像这样

EKEvent <0x7fd8ae554ba0>
{
     EKEvent <0x7fd8ae554ba0>
{    title =        E-Cold
1mg; 
     location =     ; 
     calendar =     EKCalendar <0x7fd8ae717420> {title = Medicines; type = Local; allowsModify = YES; color = #1badf8;}; 
     alarms =       (
    "EKAlarm <0x7fd8ae71bd30> {triggerInterval = -300.000000}"
); 
     URL =          (null); 
     lastModified = 2015-03-18 09:01:41 +0000; 
     startTimeZone =    Asia/Kolkata (GMT+5:30) offset 19800; 
     startTimeZone =    Asia/Kolkata (GMT+5:30) offset 19800 
}; 
     location =     ; 
     structuredLocation =   (null); 
     startDate =    2015-03-18 02:30:00 +0000; 
     endDate =      2015-04-01 02:30:00 +0000; 
     allDay =       0; 
     floating =     0; 
     recurrence =   EKRecurrenceRule <0x7fd8ae720c40> RRULE FREQ=DAILY;INTERVAL=1;UNTIL=20150401T023000Z; 
     attendees =    (null); 
     travelTime =   (null); 
     startLocation =    (null);
};

以下是我的代码

EKEvent *event4 = [EKEvent eventWithEventStore:self.eventStore];                event4.title = @“E-Cold 1mg”;
event4.startDate = pickerDate.date;                
event4.endDate = fourthEndcombDate;
EKRecurrenceEnd *endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:fourthEndcombDate];
EKRecurrenceRule *rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 end:endRecurrence];
[event4 addRecurrenceRule:rule];
event4.notes = @“Cure for Cold & Infection”;
EKAlarm *alaram4 = [EKAlarm alarmWithRelativeOffset:aInterval];
[event4 addAlarm:alaram4];
[event4 setCalendar:self.defaultCalendar];
if (event4.availability != EKEventAvailabilityNotSupported) {
   event4.availability = EKEventAvailabilityFree;
}
NSError *err4 = nil;
[self.eventStore saveEvent:event4 span:EKSpanThisEvent commit:YES error:&err4];

当我像上面的8AM一样添加事件时,它会从开始日期到结束日期多次添加12AM以及正确的事件。如图所示。

Image with Event Time and Event with 12AM

Multiple events are adding along with correct event with 12AM

是默认行为还是我需要在创建事件时修改任何内容。

预期行为:事件应该从开始结束到结束日期仅添加8AM ...

请提出建议或想法来解决这个问题..!

谢谢..!

3 个答案:

答案 0 :(得分:2)

尝试使用以下代码从startDate到End Date创建事件,并在重置事件时删除重复规则。

EKEvent *event4 = [EKEvent eventWithEventStore:self.eventStore];
event4.title = @“E-Cold 1mg”;
event4.startDate = pickerDate.date;                
event4.endDate = fourthEndcombDate;
event4.notes = @“Cure for Cold & Infection”;
EKAlarm *alaram4 = [EKAlarm alarmWithRelativeOffset:aInterval];
[event4 addAlarm:alaram4];
[event4 setCalendar:self.defaultCalendar];
if (event4.availability != EKEventAvailabilityNotSupported) {
   event4.availability = EKEventAvailabilityFree;
}
NSError *err4 = nil;
[self.eventStore saveEvent:event4 span:EKSpanThisEvent commit:YES error:&err4];

答案 1 :(得分:1)

尝试将代码的最后一行更改为:

[self.eventStore saveEvent:event4 span: EKSpanFutureEvents commit:YES error:&amp; err4];

https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/index.html#//apple_ref/swift/struct/EKSpan

答案 2 :(得分:0)

如果我说得对,那么该要求是从开始日期到结束日期的上午8点的经常性事件。 event.startdate和event.enddate是指定特定事件的意思。因此,如果你想拥有一个活动,比如从2016年7月19日到2016年8月25日重新开始,每天早上8点,请按以下方式保存活动:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void OnMouseEnter(object sender, EventArgs e)
    {
        label1.Font = new Font(label1.Font.Name, 20, FontStyle.Regular);
    }

    private void OnMouseLeave(object sender, EventArgs e)
    {
        label1.Font = new Font(label1.Font.Name, 9, FontStyle.Regular);
    }

    private void label1_Click(object sender, EventArgs e)
    {
    }
}