With this plugin Plugin.Maui.AddToCalendar
it is possible in .Net MAUI apps for the platforms iOS, macOS and Android
(Windows is currently not supported) to read out the calendar list on the corresponding device and then to create a calendar entry containing these parameters by specifying the selected calendar:
- Title of the entry
- Description of the entry
- Location
- Start date/time
- End date/time
The plugin has the following API:
public interface IAddToCalendar
{
List<string> GetCalendarList();
void CreateCalendarEvent(string title, string description, string location, DateTime startDate, DateTime endDate, string calendarName);
}
The repository also contains a small .Net MAUI demo app that includes the following features:
- Read calendar list and display it in a picker.
- Query the calendar permissions for the corresponding platform
- Adding a demo calendar entry for the current day in the selected calendar
- Use of the MVVM CommunityToolkit
- Using the .Net MAUI CommunityToolkit
- MVVM Architecture
You will need to add these entries to the info.plist
file in the platform/iOS (platform/MacCatalyst) folder:
<key>NSCalendarsUsageDescription</key>
<string>Permissions are required to add events to the calendar.</string>
You will need to add these entries to the AndroidManifest.xml
file in the platform/Android folder:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
In your app you can request these permissions like this: (please note that in Android you need to request both read and write permissions):
public async Task<PermissionStatus> CheckAndRequestReadCalendarPermission()
{
var status = await Permissions.CheckStatusAsync<Permissions.CalendarRead>();
if (status == PermissionStatus.Granted)
{
return status;
}
if (status == PermissionStatus.Denied)
{
status = await Permissions.RequestAsync<Permissions.CalendarRead>();
return status;
}
status = await Permissions.RequestAsync<Permissions.CalendarRead>();
return status;
}
public async Task<PermissionStatus> CheckAndRequestWriteCalendarPermission()
{
var status = await Permissions.CheckStatusAsync<Permissions.CalendarWrite>();
if (status == PermissionStatus.Granted)
{
return status;
}
if (status == PermissionStatus.Denied)
{
status = await Permissions.RequestAsync<Permissions.CalendarWrite>();
return status;
}
status = await Permissions.RequestAsync<Permissions.CalendarWrite>();
return status;
}
This sample code shows the usage of the API methods:
partial void OnSelectedCalendarItemChanged(string value)
{
string selectedCalendar = null;
var calendars = this.addToCalendarService.GetCalendarList();
if (calendars.Count <= 0)
{
return;
}
// get selected calendar
if (calendars.Count <= 1)
{
return;
}
foreach (var itemCalendar in calendars)
{
if (SelectedCalendarItem != itemCalendar)
{
continue;
}
selectedCalendar = itemCalendar;
break;
}
DateTime today = DateTime.Now;
var startDate = new DateTime(today.Year,
today.Month,
today.Day, 8, 0, 0);
var endDate = new DateTime(today.Year,
today.Month,
today.Day, 18, 0, 0);
this.addToCalendarService.CreateCalendarEvent("Event MAUI conference",
"Visit the MAUI conference, URL: https://learn.microsoft.com/en-US/dotnet/maui/what-is-maui",
"Redmond", startDate, endDate, this.SelectedCalendarItem);
if (!string.IsNullOrEmpty(selectedCalendar))
{
WeakReferenceMessenger.Default.Send(new CloseCalendarPickerMessage(string.Empty));
Application.Current.MainPage.DisplayAlert("Calendar registration successful", $"The event was successfully added to calendar '{selectedCalendar}'!", "OK");
}
}
Here some ScreenShots of the sample app using the Plugin.Maui.AddToCalendar
for iOS, Android and macOS:
iOS:
Android:
Mac OS: