gpt4 book ai didi

ios - 如何在iOS中的特定日期添加事件?

转载 作者:行者123 更新时间:2023-12-01 22:29:28 24 4
gpt4 key购买 nike

我有一个应用程序,需要在其中添加来自服务器的特定日期的事件。我这样做如下:

[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *strtdate=[dateFormatter dateFromString:[replacedDict valueForKey:@"Departure"]];
NSPredicate *predicateForEventOnDate = [store predicateForEventsWithStartDate:strtdate endDate:[strtdate dateByAddingTimeInterval:60*60*24] calendars:calendarArray];

[store enumerateEventsMatchingPredicate:predicateForEventOnDate usingBlock:^(EKEvent *event1, BOOL *stop) {
NSLog(@"title: %@",event1.title);
NSLog(@"hasNotes: %s",event1.hasNotes ? "YES":"NO");
NSLog(@"notes: %@",event1.notes);
NSLog(@"-----");
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:event1.title, @"title",event1.hasNotes ? event1.notes : @"",@"notes", nil];

[eventOnDate addObject:dict];
}];

NSLog(@"%@",eventOnDate);
if (eventOnDate.count > 0)
{
for (int i=0;i<[eventOnDate count];i++) {
if (![[[eventOnDate objectAtIndex:i] valueForKey:@"notes"] isEqualToString:note])
{

EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = subject; //give event title you want
event.notes = note ;
event.startDate = strtdate;
event.endDate = [event.startDate dateByAddingTimeInterval:60*60*24];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if (!err) {
}
else
{
// NSLog(@" Event not created");

}
}
else if ([[[eventOnDate objectAtIndex:i]valueForKey:@"notes"] isEqualToString:note])
{

}
}
}

但是在这里,该事件仅被添加到今天的日期,同时我也收到警告消息,例如Cal Database Change Notification已将通知更改180次。谁能告诉我我要去哪里错了

我需要在特定日期添加 Activity 吗?有人可以帮我吗?

最佳答案

检查您的日期格式是否正确。

event.startDate应该为NSDate

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; // Store this so you can access this event later
}];

阅读此博客...

http://samwize.com/2014/07/09/how-to-add-edit-and-remove-ios-calendar-events/

https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/

关于ios - 如何在iOS中的特定日期添加事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388131/

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