forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarPlugin.m
172 lines (132 loc) · 5.63 KB
/
calendarPlugin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// calendarPlugin.m
// Author: Felix Montanez
// Date: 01-17-2011
// Notes:
#import "calendarPlugin.h"
#import <EventKitUI/EventKitUI.h>
#import <EventKit/EventKit.h>
@implementation calendarPlugin
@synthesize eventStore;
@synthesize defaultCalendar;
- (CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
self = (calendarPlugin*)[super initWithWebView:theWebView];
if (self) {
//[self setup];
}
return self;
}
-(void)createEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
//Get the Event store object
EKEvent *myEvent;
EKEventStore *store;
store = [[EKEventStore alloc] init];
myEvent = [EKEvent eventWithEventStore: store];
NSString* title = [arguments objectAtIndex:1];
NSString* location = [arguments objectAtIndex:2];
NSString* message = [arguments objectAtIndex:3];
NSString* startDate = [arguments objectAtIndex:4];
NSString* endDate = [arguments objectAtIndex:5];
NSString* calendarTitle = [arguments objectAtIndex:6];
EKCalendar* calendar = nil;
if(calendarTitle == nil){
calendar = store.defaultCalendarForNewEvents;
} else {
NSIndexSet* indexes = [store.calendars indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
*stop = false;
EKCalendar* cal = (EKCalendar*)obj;
if(cal.title == calendarTitle){
*stop = true;
}
return *stop;
}];
if (indexes.count == 0) {
calendar = store.defaultCalendarForNewEvents;
} else {
calendar = [store.calendars objectAtIndex:[indexes firstIndex]];
}
}
//creating the dateformatter object
NSDateFormatter *sDate = [[[NSDateFormatter alloc] init] autorelease];
[sDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [sDate dateFromString:startDate];
NSDateFormatter *eDate = [[[NSDateFormatter alloc] init] autorelease];
[eDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myEndDate = [eDate dateFromString:endDate];
myEvent.title = title;
myEvent.location = location;
myEvent.notes = message;
myEvent.startDate = myStartDate;
myEvent.endDate = myEndDate;
myEvent.calendar = calendar;
EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-2*60*60];
[myEvent addAlarm:reminder];
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];
}
}
/***** NOT YET IMPLEMENTED BELOW ************/
//-(void)deleteEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {}
/*
-(void)findEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
EKEventStore* store = [[EKEventStore alloc] init];
EKEvent* myEvent = [EKEvent eventWithEventStore: store];
NSString *startSearchDate = [arguments objectAtIndex:1];
NSString *endSearchDate = [arguments objectAtIndex:2];
//creating the dateformatter object
NSDateFormatter *sDate = [[[NSDateFormatter alloc] init] autorelease];
[sDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myStartDate = [sDate dateFromString:startSearchDate];
NSDateFormatter *eDate = [[[NSDateFormatter alloc] init] autorelease];
[eDate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myEndDate = [eDate dateFromString:endSearchDate];
// Create the predicate
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:myStartDate endDate:myEndDate calendars:defaultCalendar];
// eventStore is an instance variable.
// Fetch all events that match the predicate.
NSArray *events = [eventStore eventsMatchingPredicate:predicate];
[self setEvents:events];
}
*/
-(void)getCalendarList:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
{
NSLog(@"In plugin method getCalendarList");
NSString *callback = [arguments objectAtIndex:0];
EKEventStore* store = [[EKEventStore alloc] init];
NSString* js = nil;
if (store != nil && store.calendars.count > 0) {
NSMutableArray *titles = [[store.calendars valueForKey:@"title"] mutableCopy];
NSLog(@"Found %i calendars", titles.count);
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:titles];
js = [result toSuccessCallbackString:callback];
} else {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"no calendars found"];
js = [result toErrorCallbackString:callback];
}
[self writeJavascript:js];
}
/*-(void)modifyEvent:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options{
EKEventViewController *eventViewController = [[EKEventViewController alloc] init];
eventViewController.event = myEvent;
eventViewController.allowsEditing = YES;
navigationController we
= [[UINavigationController alloc]
initWithRootViewController:eventViewController];
[eventViewController release];
} */
//delegate method for EKEventEditViewDelegate
-(void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
[(UIViewController*)self dismissModalViewControllerAnimated:YES];
[self release];
}
@end