简体   繁体   中英

C# Monotouch/Xamarin.iOS - Import Calendar Content

How do I import the calendar from my iPhone into my app so that I can work with the data?

I'm using C# Montouch/Xamarin.iOS.

Use EventKit .

First, you have to request access to the Calendar:

EKEventStore EventStore = new EKEventStore();
EventStore.RequestAccess (EKEntityType.Event, 
        (bool granted, NSError e) => {
                if (granted)
                        //do something here
                else
                        new UIAlertView ( "Access Denied", 
"User Denied Access to Calendar Data", null,
"ok", null).Show ();
                } );

To query all events in a timeframe, do something like this:

DateTime startDate = DateTime.Now.AddDays ( -7 );
DateTime endDate = DateTime.Now;
// the third parameter is calendars we want to look in, to use all calendars, we pass null
NSPredicate query = App.Current.EventStore.PredicateForEvents ( startDate, endDate, null );

// execute the query
EKCalendarItem[] events = App.Current.EventStore.EventsMatching ( query );

You could also use Xamarin.Mobile. This would be cross-platform.

Example from the api docs :

var abook = new AddressBook();
abook.RequestPermissions().ContinueWith (t =>
{
    if (!t.Result)
        return; // Permission denied

    var builder = new StringBuilder();

    // Full LINQ support
    foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any()))
    {
        builder.AppendLine (c.DisplayName);
        foreach (Phone p in c.Phones)
            builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number));

        builder.AppendLine();
    }

    contacts.Text = builder.ToString(); // Update UI

}, TaskScheduler.FromCurrentSynchronizationContext()); // Ensure we're on the UI Thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM