我遇到的问题是,提醒日历或事件日历(取决于首先显示弹出窗口)会创建多个日历,并且不会保存任何事件/提醒。我在plist中设置了属性。以下是我用于请求访问事件和提醒的代码:
var calendarEvent = await ANApi.FetchCalendarInfoAsync(AppCore.CurrentAppInfo.AppId);
calendarEvent.Count();
foreach (var calEvent in calendarEvent.ToList())
{
if (calEvent.Type == 1)
{
AppEvent.Current.EventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError err) =>
{
if (granted)
{
lock (_lock)
{
CreateCalendar(EKEntityType.Event);
CreateEvent(calEvent);
}
}
});
}
else
{
AppEvent.Current.EventStore.RequestAccess(EKEntityType.Reminder, (bool remGranted, NSError remErr) =>
{
if (remGranted)
{
lock (_lock)
{
CreateCalendar(EKEntityType.Reminder);
CreateReminder(calEvent);
}
}
});
}
}
如果我只收到活动或提醒,代码工作正常,但由于我收到了活动和提醒,因此无法正确创建我的日历。
这就是我创建日历的方式:
public void CreateCalendar(EKEntityType type)
{
bool calExists = false;
var appName = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleDisplayName");
//This returns 0 calendars, depending on which event's request was displayed first.
EKCalendar[] calendars = AppEvent.Current.EventStore.GetCalendars(type);
foreach (EKCalendar cal in calendars)
{
if (type == EKEntityType.Event)
{
if (PersistentLayer.Instance.GetString(Constant.CALENDAR_ID) == null)
calExists = false;
else if (cal.CalendarIdentifier == PersistentLayer.Instance.GetString(Constant.CALENDAR_ID))
calExists = true;
}
else
{
if (PersistentLayer.Instance.GetString(Constant.REMINDER_CALENDAR_ID) == null)
calExists = false;
else if (cal.CalendarIdentifier == PersistentLayer.Instance.GetString(Constant.REMINDER_CALENDAR_ID))
calExists = true;
}
}
//Create a Calendar based on the App's name. If name cannot be found use App Calendar
if (!calExists)
{
EKCalendar calendar = EKCalendar.Create(type, AppEvent.Current.EventStore);
if (appName != null)
calendar.Title = appName.ToString();
else
calendar.Title = "App";
EKSource localSource = null;
foreach (EKSource source in AppEvent.Current.EventStore.Sources)
{
if (source.SourceType == EKSourceType.CalDav)
{
localSource = source;
break;
}
}
if (localSource == null)
return;
calendar.Source = localSource;
calendar.CGColor = new CGColor(255, 255, 0);
NSError calError;
AppEvent.Current.EventStore.SaveCalendar(calendar, true, out calError);
if (calError != null)
{
this.SimpleAlert("Error saving Calender", calError.ToString(), "OK", null);
return;
}
//Store the calendar Id so we can use it when saving events
if (type == EKEntityType.Event)
PersistentLayer.Instance.Edit().PutString(Constant.CALENDAR_ID, calendar.CalendarIdentifier);
else
PersistentLayer.Instance.Edit().PutString(Constant.REMINDER_CALENDAR_ID, calendar.CalendarIdentifier);
}
}
我相信存在某种竞争条件,但我无法弄明白。我尝试了多种尝试让它发挥作用,但我没有取得任何成功。
由于
答案 0 :(得分:0)
我最终搞清楚了。首先,我打破了代码并创建了所有收到的提醒,然后是事件。
首先,在尝试创建事件时,这给了我一个Error Domain = EKCADErrorDomain Code = 1010。我解决这个问题的方法是重置事件存储。完成此操作后,日历会针对提醒和事件正确创建,并且事件和提醒都会添加到各自的日历中。