新しいカレンダーを作成し、そのカレンダーを使用してイベントなどを追加するアプリを作成しています。カレンダーは問題なく作成できますが、カレンダーが存在するかどうかを確認するためにチェックを実行しようとしています。毎回同じ名前で 2 番目のものを作成しないでください。つまり、新しいカレンダーは 1 回だけ作成します。
int 変数を設定し、ループを実行してデバイス上の各カレンダーのタイトル プロパティを確認していますが、検索しているカレンダー名の文字列が一致しても、int 変数は変更されません。
「カレンダーのチェック」コードについて私が持っているものは次のとおりです。
-(void)checkForCalendar {
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendarArray = [eventStore calendarsForEntityType:EKEntityTypeEvent];
//NSLog(@"%@", calendarArray);
for (int x = 0; x < [calendarArray count]; x++) {
EKCalendar *cal = [calendarArray objectAtIndex:x];
NSString *title = [cal title];
if ([title isEqualToString:@"AFTP"] ) {
calendarExists = 1;
}else{
calendarExists = 0;
}
}
[self createCalendar];
}
そして、ここに私が「作成」カレンダー部分のために持っているものがあります:(これはうまくいきます.intの1ではなく常に「0」を取得していcalendarExists
ます.
-(void)createCalendar {
NSLog(@"%d",calendarExists);
if (calendarExists == 0) {
EKEventStore* eventStore = [[EKEventStore alloc] init];
NSString* calendarName = @"AFTP";
EKCalendar* calendar;
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV)
{
localSource = source;
break;
}
}
if (!localSource)
return;
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
[eventStore saveCalendar:calendar commit:YES error:nil];
}
}