我正在使用 EKAlarm
创建一个 EKEvent
,如下面的代码所示。在大多数情况下,这非常有效。
但是,在有限数量的设备上,不会设置警报。事件已在日历中创建,但闹钟丢失。
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Event Title";
event.allDay = YES;
event.notes = @"Event Notes";
event.calendar = calendar ? calendar : eventStore.defaultCalendarForNewEvents;
event.startDate = date;
event.endDate = date;
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:reminderDate];
[event addAlarm:alarm];
NSError* error = nil;
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent commit:TRUE error:&error];
此时,成功为 YES,错误为 nil。此外,事件对象还有一个包含警报对象的警报数组。如果我尝试删除该警报并保存事件(应用程序崩溃,因为警报实际上不存在),这就会成为一个问题。
我无法确定受影响设备的独特之处。每次在以下设备上都会发生这种情况:
- 搭载 iOS 8.1.1 的 iPhone 6
- 搭载 iOS 8.4 的 iPhone 6
- 搭载 iOS 8.4 的 iPhone 5
并且以下没有问题:
- 搭载 iOS 8.4 的 iPhone 6
- 搭载 iOS 8.3 的 iPhone 6
- 搭载 iOS 8.0.2 的 iPhone 6
- 支持 iOS 8.1 和 iOS 8.4 的 iPhone 模拟器
- 搭载 iOS 9 的 iPhone 5
以上所有设备都是不同的物理设备。所以它似乎不是任何特定的设备型号或 iOS 版本。
我还尝试过不同的日历,包括本地日历和同步(例如 Exchange、Google)日历。在受影响的设备上,所有日历都会出现此问题。
我还尝试过设置/不设置时区覆盖和默认警报时间(设置 > 邮件、联系人、日历)。
有人遇到过这个问题吗?
更新:
我构建了一个测试应用程序,以将该问题与原始应用程序中可能发生的任何其他问题隔离开来。它在大多数设备上都能完美运行,但问题在受影响的设备上仍然存在。
以下是 [eventStore saveEvent:event span:EKSpanThisEvent commit:TRUE error:&error]
之后的 EKEvent 对象的样子。请注意,此调用返回 YES,并且 error
为零。
EKEvent Object: EKEvent <0x1742e1e00>
{
EKEvent <0x1742e1e00>
{ title = Event Title;
location = ;
calendar = EKCalendar <0x1740ab4c0> {title = Calendar; type = Exchange; allowsModify = YES; color = #CC73E1;};
alarms = (
"EKAlarm <0x170085190> {triggerDate = 2015-09-02 23:21:53 +0000}"
);
URL = (null);
lastModified = 2015-08-25 23:21:53 +0000;
startTimeZone = (null);
startTimeZone = (null)
};
location = ;
structuredLocation = (null);
startDate = 2015-09-04 14:00:00 +0000;
endDate = 2015-09-05 13:59:59 +0000;
allDay = 1;
floating = 1;
recurrence = (null);
attendees = (null);
travelTime = (null);
startLocation = (null);
};
然后我调用[eventStore重置]
,它看起来像这样:
EKEvent Object: EKEvent <0x1702e2600>
{
EKEvent <0x1702e2600>
{ title = Event Title;
location = ;
calendar = EKCalendar <0x1740ad200> {title = Calendar; type = Exchange; allowsModify = YES; color = #CC73E1;};
alarms = (null);
URL = (null);
lastModified = 2015-08-25 23:21:53 +0000;
startTimeZone = (null);
startTimeZone = (null)
};
location = ;
structuredLocation = (null);
startDate = 2015-09-04 14:00:00 +0000;
endDate = 2015-09-05 13:59:59 +0000;
allDay = 1;
floating = 1;
recurrence = (null);
attendees = (null);
travelTime = (null);
startLocation = (null);
};
更新#2
我做了更多测试,并且能够在 Exchange 同步日历上一致地重现该问题。我之前已经排除了这种可能性,因为我曾要求客户尝试选择非 Exchange 日历,但他们报告问题仍然存在。
最佳答案
据我所知,这似乎是一个 iOS 错误。我能够确定,如果我在保存 EKEvent
后添加 EKAlarm
然后再次保存,则警报将成功添加并显示为附加到日历应用程序中的事件。
因此,我可以通过在保存后重置 EKEventStore
并重新加载 EKEvent
来解决该问题,如果 EKAlarm
丢失,添加并再次保存。
if (!error) {
[eventStore reset];
event = [eventStore eventWithIdentifier:event.eventIdentifier];
if (event.alarms.count < 1) {
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:reminderDate];
[event addAlarm:alarm];
[eventStore saveEvent:event span:EKSpanThisEvent commit:TRUE error:&error];
}
}
关于ios - EKEvent 不保存 EKAlarm 但仅在某些设备上没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132104/