How to get a list of writable calendars

3 posts, 0 answers
  1. Andy
    Andy avatar
    8 posts
    Member since:
    Oct 2013

    Posted 30 Dec 2015 Link to this post

    The TelerikNEXT sample shows you how to add events to the user's default calendar. I thought any app that want to write to the calendar should allow the user to decide which of their calendars to add an event to. The below code is in a modal view that I display in response to a tap on a button labeled "Add to Calendar" when the user is viewing an event listing. While the code I'm posing here doesn't actually do anything with the information it retrieves, the real code pushes the calendar ID and title as a JSON object into an observable array that is bound to a ListView.

    var platform = require("platform");
    var appModule = require("application");

    if (platform.device.os === platform.platformNames.android) {
        var projection = java.lang.reflect.Array.newInstance(java.lang.String.class, 3);
        projection[0] = "_id";
        projection[1] = "calendar_displayName";
        projection[2] = "calendar_access_level";

        var cals = android.net.Uri.parse("content://com.android.calendar/calendars");
        var contentResolver = appModule.android.foregroundActivity.getApplicationContext().getContentResolver();
        var managedCursor = contentResolver.query(cals, projection, null, null, null);
        
        if (managedCursor !== null) {                
            while (managedCursor.moveToNext()) {
                var idCol = managedCursor.getColumnIndex(projection[0]);
                var nameCol = managedCursor.getColumnIndex(projection[1]);
                var permsCol = managedCursor.getColumnIndex(projection[2]);
                if (managedCursor.getString(permsCol) === "700") { // select from only those the user owns
                    console.log("Calendar: " + managedCursor.getString(nameCol) + ", ID: " + managedCursor.getString(idCol));
                    // Do something with the data
                    // perhaps: someObservableArray.push({ id:  managedCursor.getString(idCol), title: managedCursor.getString(nameCol) });
                }
            }
       }
    }
    else if (platform.device.os === platform.platformNames.ios) {
        var store = EKEventStore.new();
        
        store.requestAccessToEntityTypeCompletion(EKEntityTypeEvent, function (granted, error) {
            if (!granted) {
                // Close the modal dialog now
                return;
            }

            var cals = store.calendarsForEntityType(EKEntityTypeEvent); //store.sources;
            for (var i = 0; i < cals.count; i++) {
                var cal = cals.objectAtIndex(i);
                if (cal && cal.allowsContentModifications) { // Got calendar and it's writable
                    console.log("id=" + cal.calendarIdentifier + ", title=" + cal.title + ", canmodify=" + cal.allowsContentModifications);
                    // Do something with the data
                    // perhaps: someObservableArray.push({ id:  cal.calendarIdentifier, title: cal.title });
                }
            }
        });
    }

  2. Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 07 Jan Link to this post

    Hi Andy,

    Thanks for the code snippet and the explanations.

    Please make sure to post your know-how on the dedicated NativeScript Google Group here:

    https://groups.google.com/forum/#!forum/nativescript

    in this way you will make sure your post will gain more visibility among the NativeScript users.

    Regards,
    Deyan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 07 Jan Link to this post

    Hi Andy,

    you can also check this plugin - https://github.com/EddyVerbruggen/nativescript-calendar

    It is still fresh and not verified, but it maybe useful for you.

    Regards,
    Valentin.Stoychev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top