diff --git a/iOS/CalendarPlugin/README b/iOS/CalendarPlugin/README index d463cd92..972bac68 100644 --- a/iOS/CalendarPlugin/README +++ b/iOS/CalendarPlugin/README @@ -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. @@ -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"; @@ -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. diff --git a/iOS/CalendarPlugin/calendar.js b/iOS/CalendarPlugin/calendar.js index c8de9c49..4fe99a0e 100644 --- a/iOS/CalendarPlugin/calendar.js +++ b/iOS/CalendarPlugin/calendar.js @@ -4,7 +4,7 @@ // Created: 01-17-2012 // // Contributors: -// Michael Brooks +// Michael Brooks, Trevor Cox function calendarPlugin() @@ -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) { diff --git a/iOS/CalendarPlugin/calendarPlugin.h b/iOS/CalendarPlugin/calendarPlugin.h index a6a3d2f9..1c4eb2fb 100644 --- a/iOS/CalendarPlugin/calendarPlugin.h +++ b/iOS/CalendarPlugin/calendarPlugin.h @@ -17,8 +17,8 @@ @interface calendarPlugin : CDVPlugin { - EKEventStore *eventStore; - EKCalendar *defaultCalendar; + //EKEventStore *eventStore; + //EKCalendar *defaultCalendar; //NSArray *events; //future plan to have global type variables @@ -26,8 +26,8 @@ } -@property (nonatomic,retain) EKEventStore *eventStore; -@property (nonatomic,retain) EKCalendar *defaultCalendar; +//@property (nonatomic,retain) EKEventStore *eventStore; +//@property (nonatomic,retain) EKCalendar *defaultCalendar; //-(NSArray *)fetchEvents; diff --git a/iOS/CalendarPlugin/calendarPlugin.m b/iOS/CalendarPlugin/calendarPlugin.m index 68c9fc88..ff278462 100644 --- a/iOS/CalendarPlugin/calendarPlugin.m +++ b/iOS/CalendarPlugin/calendarPlugin.m @@ -10,9 +10,8 @@ #import @implementation calendarPlugin -@synthesize eventStore; -@synthesize defaultCalendar; - +//@synthesize eventStore; +//@synthesize defaultCalendar; - (CDVPlugin*) initWithWebView:(UIWebView*)theWebView { @@ -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; @@ -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){ @@ -58,17 +91,23 @@ -(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; @@ -76,24 +115,31 @@ -(void)createEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)o 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 ************/