This is a migrated thread and some comments may be shown as answers.

How to get a list of writable calendars

2 Answers 71 Views
General Discussion
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andy
Top achievements
Rank 1
Andy asked on 30 Dec 2015, 02:57 PM

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 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 07 Jan 2016, 09:31 AM
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
0
Valentin.Stoychev
Telerik team
answered on 07 Jan 2016, 09:48 AM
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
Tags
General Discussion
Asked by
Andy
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Valentin.Stoychev
Telerik team
Share this question
or