#import <UIKit/UIKit.h> #import <EventKit/EventKit.h> #import <EventKitUI/EventKitUI.h> @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, EKEventEditViewDelegate, UIAlertViewDelegate> @property (strong, nonatomic) EKEventStore *eventStore; @property (weak, nonatomic) IBOutlet UITableView *eventsTableView; @property (nonatomic, strong) NSMutableDictionary *events; @property (nonatomic, strong) NSArray *calendars; @end
1 // 2 // ViewController.m 3 // My Events App 4 // 5 // Created by Hans-Eric Grönlund on 8/27/12. 6 // Copyright (c) 2012 Hans-Eric Grönlund. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 self.title = @"Events"; 21 UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 22 initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self 23 action:@selector(refresh:)]; 24 self.navigationItem.leftBarButtonItem = refreshButton; 25 26 UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)]; 27 self.navigationItem.rightBarButtonItem = addButton; 28 29 self.eventsTableView.delegate = self; 30 self.eventsTableView.dataSource = self; 31 32 self.eventStore = [[EKEventStore alloc] init]; 33 34 [self.eventStore requestAccessToEntityType:EKEntityTypeEvent 35 completion:^(BOOL granted, NSError *error) 36 { 37 if (granted) 38 { 39 self.calendars = 40 [self.eventStore calendarsForEntityType:EKEntityTypeEvent]; 41 [self fetchEvents]; 42 } 43 else 44 { 45 NSLog(@"Access not granted: %@", error); 46 } 47 }]; 48 } 49 50 - (void)refresh:(id)sender 51 { 52 [self fetchEvents]; 53 [self.eventsTableView reloadData]; 54 } 55 56 - (void)fetchEvents 57 { 58 self.events = [[NSMutableDictionary alloc] initWithCapacity:[self.calendars count]]; 59 60 NSDate * now = [NSDate date]; 61 NSCalendar * calendar = [NSCalendar currentCalendar]; 62 63 NSDateComponents * fortyEightHoursFromNowComponents = 64 [[NSDateComponents alloc] init]; 65 fortyEightHoursFromNowComponents.day = 2; // 48 hours forward 66 NSDate * fortyEightHoursFromNow = 67 [calendar dateByAddingComponents:fortyEightHoursFromNowComponents toDate:now 68 options:0]; 69 70 71 72 for (EKCalendar * calendar in self.calendars) 73 { 74 NSPredicate * allEventsWithin48HoursPredicate = 75 [self.eventStore predicateForEventsWithStartDate:now 76 endDate:fortyEightHoursFromNow calendars:@[calendar]]; 77 NSArray * eventsInThisCalendar = 78 [self.eventStore eventsMatchingPredicate:allEventsWithin48HoursPredicate]; 79 if (eventsInThisCalendar != nil) 80 { 81 [self.events setObject:eventsInThisCalendar forKey:calendar.title]; 82 } 83 } 84 dispatch_async(dispatch_get_main_queue(),^{ 85 [self.eventsTableView reloadData]; 86 }); 87 } 88 89 - (void)addEvent:(id)sender 90 { 91 UIAlertView * inputAlert = [[UIAlertView alloc] initWithTitle:@"New Event" message:@"Enter a title for the event" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 92 inputAlert.alertViewStyle = UIAlertViewStylePlainTextInput; 93 [inputAlert show]; 94 } 95 96 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 97 { 98 if (buttonIndex == 1) 99 { 100 // OK button tapped 101 102 // Calculate the date exactly one day from now 103 NSCalendar * calendar = [NSCalendar currentCalendar]; 104 NSDateComponents * aDayFromNowComponents = [[NSDateComponents alloc] init]; 105 aDayFromNowComponents.day = 1; 106 NSDate * now = [NSDate date]; 107 NSDate * aDayFromNow = [calendar dateByAddingComponents:aDayFromNowComponents toDate:now options:0]; 108 109 // Create the event 110 EKEvent * event = [EKEvent eventWithEventStore:self.eventStore]; 111 event.title = [alertView textFieldAtIndex:0].text; 112 event.calendar = [self.eventStore defaultCalendarForNewEvents]; 113 event.startDate = aDayFromNow; 114 event.endDate = [NSDate dateWithTimeInterval:60*60.0 sinceDate:event.startDate]; 115 116 // Make it recur 117 EKRecurrenceRule *repeatEveryMondayAndTuesdayRecurrenceRule = [[EKRecurrenceRule alloc] 118 initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily 119 interval:2 120 daysOfTheWeek:@[[EKRecurrenceDayOfWeek dayOfWeek:2], [EKRecurrenceDayOfWeek dayOfWeek:3]] 121 daysOfTheMonth:nil 122 monthsOfTheYear:nil 123 weeksOfTheYear:nil 124 daysOfTheYear:nil 125 setPositions:nil 126 end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:20]]; 127 event.recurrenceRules = @[repeatEveryMondayAndTuesdayRecurrenceRule]; 128 129 // Save the event and update the table view 130 [self.eventStore saveEvent:event span:EKSpanThisEvent error:nil]; 131 [self refresh:self]; 132 } 133 } 134 135 - (EKCalendar *)calendarAtSection:(NSInteger)section 136 { 137 return [self.calendars objectAtIndex:section]; 138 } 139 140 - (EKEvent *)eventAtIndexPath:(NSIndexPath *)indexPath 141 { 142 EKCalendar *calendar = [self calendarAtSection:indexPath.section]; 143 NSArray *calendarEvents = [self eventsForCalendar:calendar]; 144 return [calendarEvents objectAtIndex:indexPath.row]; 145 } 146 147 - (NSArray *)eventsForCalendar:(EKCalendar *)calendar 148 { 149 return [self.events objectForKey:calendar.title]; 150 } 151 152 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 153 { 154 return [self.calendars count]; 155 } 156 157 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 158 { 159 return [self calendarAtSection:section].title; 160 } 161 162 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 163 { 164 EKCalendar *calendar = [self calendarAtSection:section]; 165 return [self eventsForCalendar:calendar].count; 166 } 167 168 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 169 { 170 static NSString *CellIdentifier = @"Cell"; 171 172 UITableViewCell *cell = 173 [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 174 if (cell == nil) 175 { 176 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 177 reuseIdentifier:CellIdentifier]; 178 } 179 180 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 181 cell.textLabel.backgroundColor = [UIColor clearColor]; 182 cell.textLabel.font = [UIFont systemFontOfSize:19.0]; 183 184 cell.textLabel.text = [self eventAtIndexPath:indexPath].title; 185 186 return cell; 187 } 188 189 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 190 { 191 EKEventViewController *eventVC = [[EKEventViewController alloc] init]; 192 eventVC.event = [self eventAtIndexPath:indexPath]; 193 eventVC.allowsEditing = YES; 194 [self.navigationController pushViewController:eventVC animated:YES]; 195 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 196 } 197 198 - (void)viewWillAppear:(BOOL)animated 199 { 200 [self refresh:self]; 201 [super viewWillAppear:animated]; 202 } 203 204 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 205 { 206 EKEventEditViewController *eventEditVC = [[EKEventEditViewController alloc] init]; 207 eventEditVC.event = [self eventAtIndexPath:indexPath]; 208 eventEditVC.eventStore = self.eventStore; 209 eventEditVC.editViewDelegate = self; 210 [self presentViewController:eventEditVC animated:YES completion:nil]; 211 } 212 213 -(void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action 214 { 215 [self dismissViewControllerAnimated:YES completion:nil]; 216 } 217 218 -(EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents: 219 (EKEventEditViewController *)controller 220 { 221 return [self.eventStore defaultCalendarForNewEvents]; 222 } 223 224 - (void)didReceiveMemoryWarning 225 { 226 [super didReceiveMemoryWarning]; 227 // Dispose of any resources that can be recreated. 228 } 229 230 @end
时间: 2024-10-15 23:53:38