gpt4 book ai didi

ios - 如何在 iOS 中更新或编辑 EKEvent 并使用标识符存储在 native 日历中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:28 25 4
gpt4 key购买 nike

我在我的应用程序中使用 EKEvent 来获取所有商店事件,我想通过标识符编辑任何特定事件并重新保存到现有的..我应该怎么做?

EKEventStore *store = [EKEventStore new];
EKEvent *event = [EKEvent eventWithEventStore:store];

event.title = @"abc";
event.notes= @"this is updated notes";

event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;

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

这段代码是我用来存储事件的,但是在哪里传递标识符以仅更新特定事件?

最佳答案

您应该将事件标识符保存在数据库中,以便您以后可以使用它来更新或删除事件。使用创建事件后:

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

您可以访问 event.eventIdentifier。将其保存在数据库中。当您想编辑特定事件时,只需使用存储的 ID 获取该事件:

-(void)updateNotification:(NSMutableDictionary *) info
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
{
dispatch_async(dispatch_get_main_queue(), ^{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot access Calendar" message:@"Please give the permission to add task in calendar from iOS > Settings > Privacy > Calendars" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];

});
return;
}

if (error)
{
NSLog(@"%@", error);
}

//this is event ID you saved in DB. now you want to edit that event
EKEvent *event = [eventStore eventWithIdentifier:info[@"eventID"]];

if(event) {

//You have your valid event. Modify as per your need.

event.title = [info valueForKey:@"title"];
event.notes = [info valueForKey:@"description"];
event.startDate = [info objectForKey:@"date"];
event.endDate = [[NSDate alloc] initWithTimeInterval:3600*24 sinceDate:event.startDate];
event.calendar = eventStore.defaultCalendarForNewEvents;

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

}

}];
}

希望对您有所帮助。如有任何疑问,请随时提出。

关于ios - 如何在 iOS 中更新或编辑 EKEvent 并使用标识符存储在 native 日历中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215906/

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