如何在 ios 日历中的 EKEvent 中设置特定的警报?

How to set specific alarm in EKEvent in ios calendar?

在 EKEvent 中,一旦我没有为事件设置任何闹钟,然后我检查日历应用程序,它会在 5 分钟前提醒我。为什么 ?没看懂

我的要求

我应该做什么?

给出任何建议、解决方案、想法

谢谢

       let event:EKEvent = EKEvent(eventStore: appleEventStore)

        event.title = tempDescription
        event.startDate = startDate as Date
        event.endDate = endDate as Date
        event.isAllDay = false
        event.notes = "This is a note"
        event.calendar = cal
        do {
            try appleEventStore.save(event, span: .thisEvent)
            print("events added with dates:")

            let alarm = EKAlarm(relativeOffset: 600)
            event.addAlarm(alarm)

            switch tempalertid {

            case "1":
                let aInterval: TimeInterval = -5 * 60
                let alaram = EKAlarm(relativeOffset: aInterval)
                event.addAlarm(alaram)
            case "2":
                let aInterval: TimeInterval = -15 * 60
                let alaram = EKAlarm(relativeOffset: aInterval)
                event.addAlarm(alaram)
            case "3":
                let aInterval: TimeInterval = -30 * 60
                let alaram = EKAlarm(relativeOffset: aInterval)
                event.addAlarm(alaram)
            case "4":
                let aInterval: TimeInterval = -1 * 60 * 60
                let alaram = EKAlarm(relativeOffset: aInterval)
                event.addAlarm(alaram)

            default:
                break
            }
            if event.hasAlarms {
                print("yes")

            }
            else {
                print("no")
            }

        } catch let e as NSError {
            print(e.description)
            return
        }
        print("Saved Event")

我有两个想法:

首先,您要在创建闹钟之前保存事件。

其次,.alarm 属性 是一个警报数组,您正试图单独保存每个警报。

我有一个代码示例供您查看,不幸的是,它是用 Objective-C 编写的。

EKEvent *event = [EKEvent eventWithEventStore:store];
EKAlarm *alarmForOneDayBeforeAppointment = [[EKAlarm alloc] init];
alarmForOneDayBeforeAppointment.absoluteDate = [self modifyDate:eventDate
                                                       withUnit:NSCalendarUnitDay
                                                                 andQuantity:-1];
event.alarms = @[alarmForOneDayBeforeAppointment];
[store saveEvent:event span:EKSpanThisEvent error:&err];

将 event.alarm 设置为数组后,尝试保存您的活动。

    let event:EKEvent = EKEvent(eventStore: appleEventStore)
    event.title = tempDescription
    event.startDate = startDate as Date
    event.endDate = endDate as Date
    event.isAllDay = false
    event.notes = "This is a note"
    event.calendar = cal


    switch tempalertid {
        case "1":
            let aInterval: TimeInterval = -5 * 60
            let alaram = EKAlarm(relativeOffset: aInterval)
             event.alarms = [alaram]
        case "2":
            let aInterval: TimeInterval = -15 * 60
            let alaram = EKAlarm(relativeOffset: aInterval)
             event.alarms = [alaram]
        case "3":
            let aInterval: TimeInterval = -30 * 60
            let alaram = EKAlarm(relativeOffset: aInterval)
             event.alarms = [alaram]
        case "4":
            let aInterval: TimeInterval = -1 * 60 * 60
            let alaram = EKAlarm(relativeOffset: aInterval)
            event.alarms = [alaram]

        default:
            break
        }

    do {
        try appleEventStore.save(event, span: .thisEvent)
        print("events added with dates:")

    } catch let e as NSError {
        print(e.description)
        return
    }
    print("Saved Event")