Đây là cách bạn có thể kiểm tra liệu một lịch đã tồn tại với tiêu đề cụ thể. Nếu nó không tồn tại thì bạn có thể tạo nó theo chương trình.
Khai báo một loại Boolean Biến
BOOL doesExist=NO;
EKEventStore *eventStore=[[EKEventStore alloc] init];
NSArray *calanders=[eventStore calendarsForEntityType:EKEntityTypeEvent];
//Now Iterate through every calendar in the array and match its title
// with the title that you want to create
for(EKCalendar calendar in calendars)
{
if([[calendar title] isEqualToString:@"youdesiredname"])
{
doesExist=YES;
}
}
// vì vậy bây giờ kiểm tra xem biến bool của chúng tôi chứa giá trị YES nó có nghĩa là một cuốn lịch với cùng tên/title đã exists.if không có thì bạn có thể tạo
if(!doesExist)
{
NSString* calendarName = @"DesiredCalendarName";
EKCalendar* calendar;
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
if (!localSource)
return;
calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
calendar = [eventStore calendarWithIdentifier:self.calendarIdentifier];
event.calendar = calendar;
// Set the start date to the current date/time and the event duration to one hour
NSDate *startDate = [NSDate date];
event.startDate = startDate;
event.endDate = [startDate dateByAddingTimeInterval:3600];
//And to save the event to the event database:
NSError *error = nil;
BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (result)
{
NSLog(@"Saved event to event store.")
}
else
{
NSLog(@"Error saving event: %@.", saveError);
}
NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
}
}
Làm cách nào bạn kiểm tra xem lịch đã tồn tại chưa? Tôi sẽ không muốn tạo ra lịch liên tục một cách tình cờ? – Slee
Khi bạn tạo lịch, có một thuộc tính có tên là calendarIdentifier là một định danh duy nhất. Lưu trữ mã định danh duy nhất đó (và mã định danh của sự kiện quá - đảm bảo rằng không thay đổi) và kiểm tra cửa hàng sự kiện để xem nó có tồn tại hay không trước khi tạo nó. – kurtzmarc
Nhưng đây là bắt: Tôi thấy rằng nếu ứng dụng của tôi tạo lịch địa phương, người dùng không thể xóa nó sau này. Điều đó có vẻ như một lỗi. – matt