gpt4 book ai didi

ios - EKEvent 将相同的事件添加到日历

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:17 25 4
gpt4 key购买 nike

我一直在测试此代码以将日历添加到 IOS Cal 应用程序,并将一些事件添加到应用程序中的特定日历。

我有一个装有 IOS7 的 Ipad,禁用了 iCloud。

第一个问题是当我创建日历时,我在 iPad 的 iCal 应用程序中看不到新创建的日历,即使没有添加到 iCloud,该应用程序是否应该显示所有日历?

第二个问题是我的代码每次调用该代码时都会添加相同的事件。

假设我调用了 add 函数,它第一次成功地添加了 3 个事件,下次当用相同的事件调用该函数时,它应该跳过添加,但它再次将这 3 个事件添加到日历,现在我有重复的事件,所以在...

-(void) initCalendar:(NSDictionary *)dataDict
{
self.eventStore = [[EKEventStore alloc] init];
if([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.isCalendarAccepted=YES;
NSLog(@"self.isCalendarAccepted=YES;");
if ([defaults objectForKey:@"Calendar"] == nil) // Create Calendar if Needed
{
EKSource *theSource = nil;

for (EKSource *source in self.eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
theSource = source;
NSLog(@"iCloud Store Source");
break;
} else {
for (EKSource *source in self.eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
theSource = source;
NSLog(@"ios Local Store Source");
break;
}
}
}
}


//EKCalendar *calendar = [EKCalendar calendarWithEventStore:self.eventStore];
EKCalendar *calendar =[EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
calendar.title = @"My App Name";
if (theSource) {
calendar.source = theSource;
} else {
NSLog(@"Error: Local source not available");
return;
}
NSError *errorCalendar = nil;
BOOL result = [self.eventStore saveCalendar:calendar commit:YES error:&errorCalendar];
if (result) {
NSLog(@"Saved calendar to event store.");
[[NSUserDefaults standardUserDefaults] setObject:calendar.calendarIdentifier forKey:@"Calendar"];
[[NSUserDefaults standardUserDefaults] synchronize];


} else {
NSLog(@"Error saving calendar: %@.", errorCalendar);
}

}
//start adding event to calendar
[self addSelectedOwnEventsToCalendar:dataDict];
}
}];
}


}

-(void)addSelectedOwnEventsToCalendar:(NSDictionary *)dataDict
{

// Create a new event... save and commit
NSError *error = nil;

EKEvent *myEvent = [EKEvent eventWithEventStore:self.eventStore];


myEvent.allDay = NO;
myEvent.availability = EKEventAvailabilityFree;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [[NSDate alloc] init];
NSString *startDateFormatted=[NSString stringWithFormat:@"%@ %@",[dataDict objectForKey:@"start_date"],[dataDict objectForKey:@"starts"]];
startDate = [dateFormatter dateFromString:startDateFormatted];

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *endDate = [[NSDate alloc] init];
NSString *endDateFormatted=[NSString stringWithFormat:@"%@ %@",[dataDict objectForKey:@"end_date"],[dataDict objectForKey:@"ends"]];
endDate = [dateFormatter1 dateFromString:endDateFormatted];

//compare dates
NSComparisonResult result = [startDate compare:endDate];
if (result == NSOrderedAscending) {
} else if (result == NSOrderedDescending) {
} else {
//the same
endDate=[startDate dateByAddingTimeInterval:60];
}

myEvent.startDate=startDate;
myEvent.endDate = endDate;
myEvent.title = [dataDict objectForKey:@"event_id"];
myEvent.calendar = [self.eventStore calendarWithIdentifier:[[NSUserDefaults standardUserDefaults] objectForKey:@"Calendar"]];
myEvent.location=[dataDict objectForKey:@"location"];



NSArray *cals = [self.eventStore calendarsForEntityType: EKEntityTypeEvent];
NSPredicate *predicateForEventsOnHolidayDate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:cals]; // nil will search through all calendars

NSArray *eventsOnHolidayDate = [self.eventStore eventsMatchingPredicate:predicateForEventsOnHolidayDate];
//NSLog(@"eventsOnHolidayDate %@",eventsOnHolidayDate);

BOOL eventExists = NO;

for (EKEvent *eventToCheck in eventsOnHolidayDate) {
NSLog(@"eventToCheck.title %@",eventToCheck.title);
NSLog(@"event_id %@",[dataDict objectForKey:@"event_id"]);
if ([eventToCheck.title isEqualToString:[dataDict objectForKey:@"event_id"]]) {
eventExists = YES;
NSLog(@"Event Already Exists");
}
}
//save eventts
if (eventExists == NO) {
[self.eventStore saveEvent:myEvent span:EKSpanThisEvent commit:YES error:&error];
if (!error) {
// NSLog(@"the event saved and committed correctly with identifier %@", myEvent.eventIdentifier);

} else {
NSLog(@"there was an error saving and committing the event");
NSLog(@"Error %@",error);
error = nil;

}


}
}

当我第一次运行代码时,NSlog 显示 (@"ios Local Store Source");

我错过了什么?

注意:代码大部分时间都在模拟器(Ipad ios 7)上运行,我假设 [NSUserDefaults] 有问题?

最佳答案

首先,请参阅 calendarItemExternalIdentifier 中关于重复项的讨论要点文档并确定它们是否适用。

然后,看看下面的建议:

  1. calendar identifier可以换。确保 NSUserDefaults 中 key Calendar 的保存版本与 addSelectedOwnEventsToCalendar: 中访问的版本相同。
  2. NSUserDefaults 检索日历标识符时,确保它是有效的。
  3. 尝试使用不带commit 参数的saveEvent:span:error: 方法。我在过去使用采用提交参数的保存事件方法时遇到过问题。

最后,我认为 iOS 日历访问授权只能在 iOS 设备上进行测试,这可能就是它在模拟器中运行的原因。

关于ios - EKEvent 将相同的事件添加到日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124057/

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