Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed for iOS 6, all-day events, reminder param, success/failure callbacks #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions iOS/CalendarPlugin/README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Author: Felix Montanez
//
// Collaborators:
// Michael Brooks
// Michael Brooks, Trevor Cox
//
// Notes: this is a basic plugin which creates an event to the user's local calendar. There are methods that I'm working on for fetching an event, modifying and deleting
// events in the calendar.
Expand All @@ -24,7 +24,7 @@
// var cal;
// cal = window.plugins.calendarPlugin
//
// function call in Javascript:
// function call in Javascript with just the required parameters:
//
// createEvent : function(title,location,notes, startDate, endDate, calendarName){
// var title= "My Appt";
Expand All @@ -38,18 +38,28 @@
// },
//
//
// Which can also be written as:
// Which can also be written (with optional parameters):
//
// function createEvent(title,location,notes, startDate, endDate, calendarName){
// var title= "My Appt";
// var location = "Los Felix";
// var notes = "me testing";
// var startDate = "2012-01-23 09:30:00";
// var endDate = "2012-01-23 12:30:00";
// var calendarName = "Work"; // optional
//
// cal.createEvent(title,location,notes,startDate,endDate, calendarName);
// }
// var startDate = "2012-01-23 09:30:00"; // or "2012-01-23" for all-day
// var endDate = "2012-01-23 12:30:00"; // or "2012-01-23" for all-day
// var reminder = 60; //minutes
// cal.createEvent(title,location,notes,startDate,endDate,reminder,
// function() {
// // success
// alert('Added to your Calendar.');
// },
// function(errmsg) {
// // failure
// alert(errmsg);
// });
// }
//
//
//
//
// Also, there is a method "getCalendarList" which provides a list of calendar titles if you want
// pass in the title as the last parameter to createEvent to specify a calendar other than the default.
Expand Down
8 changes: 4 additions & 4 deletions iOS/CalendarPlugin/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created: 01-17-2012
//
// Contributors:
// Michael Brooks
// Michael Brooks, Trevor Cox


function calendarPlugin()
Expand All @@ -13,9 +13,9 @@ function calendarPlugin()



calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate,calendarName) {
console.log("creating event");
cordova.exec(null,null,"calendarPlugin","createEvent", [title,location,notes,startDate,endDate,calendarName]);
calendarPlugin.prototype.createEvent = function(title,location,notes,startDate,endDate,calendarName,reminderMinutes,success,fail) {
console.log("calendarPlugin.createEvent");
cordova.exec(success,fail,"calendarPlugin","createEvent", [title,location,notes,startDate,endDate,calendarName,reminderMinutes]);
};

calendarPlugin.prototype.getCalendarList = function(response, err) {
Expand Down
8 changes: 4 additions & 4 deletions iOS/CalendarPlugin/calendarPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

@interface calendarPlugin : CDVPlugin <EKEventEditViewDelegate> {

EKEventStore *eventStore;
EKCalendar *defaultCalendar;
//EKEventStore *eventStore;
//EKCalendar *defaultCalendar;
//NSArray *events;

//future plan to have global type variables


}

@property (nonatomic,retain) EKEventStore *eventStore;
@property (nonatomic,retain) EKCalendar *defaultCalendar;
//@property (nonatomic,retain) EKEventStore *eventStore;
//@property (nonatomic,retain) EKCalendar *defaultCalendar;

//-(NSArray *)fetchEvents;

Expand Down
88 changes: 67 additions & 21 deletions iOS/CalendarPlugin/calendarPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#import <EventKit/EventKit.h>

@implementation calendarPlugin
@synthesize eventStore;
@synthesize defaultCalendar;

//@synthesize eventStore;
//@synthesize defaultCalendar;

- (CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
Expand All @@ -25,6 +24,39 @@ - (CDVPlugin*) initWithWebView:(UIWebView*)theWebView

-(void)createEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
EKEventStore *eventStore1 = [[EKEventStore alloc] init];
// iOS 6.0 prompts user before allowing calendar access.
if ([eventStore1 respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// Note: To test access prompt, uninstall won't reset; must use Settings.app / General / Reset / Reset Location & Privacy
[eventStore1 requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self createEventImpl:arguments withDict:options];
} else
{
NSLog(@"%@", error);
NSString* callbackId = [arguments objectAtIndex:0];
NSString* javaScript = nil;
CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:
@"You have not given permission for this app to access the calendar. You can change this in your Settings (Privacy->Calendar)."];
javaScript = [pluginResult toErrorCallbackString:callbackId];
[self performSelector:@selector(writeJavascript:) onThread:[NSThread mainThread] withObject:javaScript waitUntilDone:NO];
}
[eventStore1 release];
}];
}
else {
[self createEventImpl:arguments withDict:options];
[eventStore1 release];
}
}

-(BOOL)createEventImpl:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
NSString* callbackId = [arguments objectAtIndex:0];
NSString* javaScript = nil;
CDVPluginResult* pluginResult = nil;

//Get the Event store object
EKEvent *myEvent;
EKEventStore *store;
Expand All @@ -38,6 +70,7 @@ -(void)createEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)o
NSString* startDate = [arguments objectAtIndex:4];
NSString* endDate = [arguments objectAtIndex:5];
NSString* calendarTitle = [arguments objectAtIndex:6];
NSString *reminderMinutes = [arguments objectAtIndex:7];

EKCalendar* calendar = nil;
if(calendarTitle == nil){
Expand All @@ -58,42 +91,55 @@ -(void)createEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)o
}
}

//creating the dateformatter object
NSString *dateFmt;
if([startDate length] == 19) {
dateFmt = @"yyyy-MM-dd HH:mm:ss";
}
else {
myEvent.allDay = YES;
dateFmt = @"yyyy-MM-dd";
}

NSDateFormatter *sDate = [[[NSDateFormatter alloc] init] autorelease];
[sDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[sDate setDateFormat:dateFmt];
NSDate *myStartDate = [sDate dateFromString:startDate];



NSDateFormatter *eDate = [[[NSDateFormatter alloc] init] autorelease];
[eDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[eDate setDateFormat:dateFmt];
NSDate *myEndDate = [eDate dateFromString:endDate];


myEvent.title = title;
myEvent.location = location;
myEvent.notes = message;
myEvent.startDate = myStartDate;
myEvent.endDate = myEndDate;
myEvent.calendar = calendar;

if(reminderMinutes != nil) {
EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-60*[reminderMinutes intValue]];
[myEvent addAlarm:reminder];
}

EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-2*60*60];

[myEvent addAlarm:reminder];
NSLog(@"%@", myEvent);

NSError *error;
BOOL saved = [store saveEvent:myEvent span:EKSpanThisEvent
error:&error];
if (saved) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:@"Saved to Calendar" delegate:self
cancelButtonTitle:@"Thank you!"
otherButtonTitles:nil];
[alert show];
[alert release];



if(saved) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
javaScript = [pluginResult toSuccessCallbackString:callbackId];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:
[NSString stringWithFormat:@"%@", [error localizedDescription]]];
javaScript = [pluginResult toErrorCallbackString:callbackId];
}
[self performSelector:@selector(writeJavascript:) onThread:[NSThread mainThread] withObject:javaScript waitUntilDone:NO];

[store release];

return saved;
}

/***** NOT YET IMPLEMENTED BELOW ************/
Expand Down