Last active
August 29, 2015 14:04
-
-
Save wwwins/20b3e9118a5be0c3e234 to your computer and use it in GitHub Desktop.
iOS calendar manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HVPBaseCalendarViewController.h | |
// | |
// Created by wwwins on 2014/8/5. | |
// Copyright (c) 2014年 isobar. All rights reserved. | |
// | |
/* | |
Usage: | |
__weak HVPBaseCalendarViewController *weakSelf = self; | |
[self checkEventStoreAccessForCalendar:^(){ | |
// 取得行事曆權限後執行 addEventAt | |
[weakSelf addEventAt:[NSDate date]] withTitle:@"Title" withNotes:@"Description" inLocation:@"Place"]; | |
}]; | |
*/ | |
#import <EventKit/EventKit.h> | |
#import <EventKitUI/EventKitUI.h> | |
#import "HVPBaseViewController.h" | |
@interface HVPBaseCalendarViewController : HVPBaseViewController <EKEventEditViewDelegate> | |
// EKEventStore instance associated with the current Calendar application | |
@property (nonatomic, strong) EKEventStore *eventStore; | |
// Default calendar associated with the above event store | |
@property (nonatomic, strong) EKCalendar *defaultCalendar; | |
@property (nonatomic, copy) void(^completion)(); | |
- (void)checkEventStoreAccessForCalendar:(void(^)())completion; | |
- (void)addEventAt:(NSDate *)startDate withTitle:(NSString *)title withNotes:(NSString *)notes inLocation:(NSString *)location; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HVPBaseCalendarViewController.m | |
// | |
// Created by wwwins on 2014/8/5. | |
// Copyright (c) 2014年 isobar. All rights reserved. | |
// | |
#import "HVPBaseCalendarViewController.h" | |
@interface HVPBaseCalendarViewController () | |
@end | |
@implementation HVPBaseCalendarViewController | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
// Initialize the event store | |
if (!_eventStore) { | |
_eventStore = [[EKEventStore alloc] init]; | |
} | |
_completion = nil; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - | |
#pragma mark Access Calendar | |
// Check the authorization status of our application for Calendar | |
- (void)checkEventStoreAccessForCalendar | |
{ | |
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; | |
switch (status) | |
{ | |
// Update our UI if the user has granted access to their Calendar | |
case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar]; | |
break; | |
// Prompt the user for access to Calendar if there is no definitive answer | |
case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess]; | |
break; | |
// Display a message if the user has denied or restricted access to Calendar | |
case EKAuthorizationStatusDenied: | |
case EKAuthorizationStatusRestricted: | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" message:@"Permission was not granted for Calendar" | |
delegate:nil | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
[alert show]; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
- (void)checkEventStoreAccessForCalendar:(void(^)())completion | |
{ | |
_completion = completion; | |
[self checkEventStoreAccessForCalendar]; | |
} | |
// Prompt the user for access to their Calendar | |
-(void)requestCalendarAccess | |
{ | |
[_eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) | |
{ | |
if (granted) | |
{ | |
__weak HVPBaseCalendarViewController *weakSelf = self; | |
// Let's ensure that our code will be executed from the main queue | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// The user has granted access to their Calendar; let's populate our UI with all events occuring in the next 24 hours. | |
[weakSelf accessGrantedForCalendar]; | |
}); | |
} | |
}]; | |
} | |
// This method is called when the user has granted permission to Calendar | |
-(void)accessGrantedForCalendar | |
{ | |
// Let's get the default calendar associated with our event store | |
_defaultCalendar = _eventStore.defaultCalendarForNewEvents; | |
_completion(); | |
} | |
#pragma mark - | |
#pragma mark Add a new event | |
// Display an event edit view controller when the user taps the "+" button. | |
// A new event is added to Calendar when the user taps the "Done" button in the above view controller. | |
- (void)addEventAt:(NSDate *)startDate withTitle:(NSString *)title withNotes:(NSString *)notes inLocation:(NSString *)location | |
{ | |
// Create an instance of EKEventEditViewController | |
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init]; | |
// create a event | |
EKEvent *event = [EKEvent eventWithEventStore:_eventStore]; | |
event.title = title; | |
event.notes = notes; | |
event.location = location; | |
event.startDate = startDate; | |
event.endDate = [startDate dateByAddingTimeInterval:3600*24]; | |
// Set addController's event store to the current event store | |
addController.event = event; | |
addController.eventStore = _eventStore; | |
addController.editViewDelegate = self; | |
[self presentViewController:addController animated:YES completion:nil]; | |
} | |
#pragma mark - | |
#pragma mark EKEventEditViewDelegate | |
// Overriding EKEventEditViewDelegate method to update event store according to user actions. | |
- (void)eventEditViewController:(EKEventEditViewController *)controller | |
didCompleteWithAction:(EKEventEditViewAction)action | |
{ | |
NSError *error = nil; | |
EKEvent *thisEvent = controller.event; | |
switch (action) { | |
case EKEventEditViewActionCanceled: | |
// Edit action canceled, do nothing. | |
break; | |
case EKEventEditViewActionSaved: | |
// When user hit "Done" button, save the newly created event to the event store, | |
// and reload table view. | |
// If the new event is being added to the default calendar, then update its | |
// eventsList. | |
[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error]; | |
break; | |
case EKEventEditViewActionDeleted: | |
// When deleting an event, remove the event from the event store, | |
// and reload table view. | |
// If deleting an event from the currenly default calendar, then update its | |
// eventsList. | |
[controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error]; | |
break; | |
default: | |
break; | |
} | |
// Dismiss the modal view controller | |
[controller dismissViewControllerAnimated:YES completion:nil]; | |
} | |
// Set the calendar edited by EKEventEditViewController to our chosen calendar - the default calendar. | |
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller | |
{ | |
return _defaultCalendar; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment