gpt4 book ai didi

ios - 如何在 iPhone 日历上创建提醒和存储提醒

转载 作者:行者123 更新时间:2023-11-28 19:38:07 26 4
gpt4 key购买 nike

我是编程新手,我制作了一个提醒应用程序,它将事件保存在 iCal 上,现在我想通过项目中的编码删除该事件,我在 iCal 中创建事件的代码如下-

-(void) setReminderInPhone {
NSString *dateWithTime = [NSString stringWithFormat:@"%@ %@",calenderDtl.dateInString,calenderDtl.openTimeUTC];
NSString *mrktName = mrktDtl.marketName;
//dateWithTime = @"2016-04-02 04:10 am";

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
[fmt setAMSymbol:@"am"];
[fmt setPMSymbol:@"pm"];
fmt.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *utc = [fmt dateFromString:dateWithTime];

fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSDate *localDate = [fmt dateFromString:local];
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = mrktName;
event.startDate = localDate; //test
event.endDate = [event.startDate dateByAddingTimeInterval:60*2];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}

最佳答案

尝试按照以下步骤从日历中删除事件。

EKEvent *eventToRemove = [eventStore eventWithIdentifier:eventIdentifier];

if (eventToRemove)
{
NSError* error = nil;

[eventStore removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
}

在 iCalendar 中创建事件时,您将获得 eventIdentifier 的值,如下所示:

//Adding Events to default iOS calendar for Making Reminders
-(NSString *)addEventToCalendar: (NSString *)title withDate:(NSString *)date{

EKEvent *event = [EKEvent eventWithEventStore:eventStore];

event.title = [NSString stringWithFormat:@"Pregnancy Workout Advisor - Event: %@",title];

event.startDate = [dateFormatter dateFromString:date];

event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting

event.allDay = YES;

[dateFormatter setDateFormat:@"YYYY-MM-dd"];

EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeInterval:9*60*60 sinceDate:[dateFormatter dateFromString:date]]];

[event addAlarm:alarm];

event.calendar = [eventStore defaultCalendarForNewEvents];

NSError *err = nil;

[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

return event.eventIdentifier; }

在上述方法中,您将获得要添加到日历的每个事件的 eventIdentifier。将 eventIdentifier 存储到 CoreData/Sqlite,无论如何..

因此,当您需要删除该事件时,只需提供该事件标识符即可。这肯定会从日历中删除。

希望对你有帮助

关于ios - 如何在 iPhone 日历上创建提醒和存储提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com