Created
October 3, 2014 14:21
-
-
Save alex1704/90336beb086daae2456b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@interface HCEventHelper()<EKEventEditViewDelegate> | |
@property (strong, nonatomic) EKEventStore *eventStore; | |
@property (weak, nonatomic) UIViewController *presentingViewController; | |
@property (weak, nonatomic) Session *session; | |
@end | |
@implementation HCEventHelper | |
#pragma mark - Interface | |
+(HCEventHelper *)sharedInstance | |
{ | |
static HCEventHelper *sharedInstance = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedInstance = [[HCEventHelper alloc] init]; | |
}); | |
return sharedInstance; | |
} | |
- (void)presentInViewController:(UIViewController *)viewController withSession:(Session *)session | |
{ | |
self.presentingViewController = viewController; | |
self.session = session; | |
[self checkEventStoreAccessForCalendar]; | |
} | |
#pragma mark - Private | |
-(void)checkEventStoreAccessForCalendar | |
{ | |
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; | |
switch (status) | |
{ | |
case EKAuthorizationStatusAuthorized: | |
[self provideCalendarAccess]; | |
break; | |
case EKAuthorizationStatusNotDetermined: | |
[self requestCalendarAccess]; | |
break; | |
case EKAuthorizationStatusDenied: | |
case EKAuthorizationStatusRestricted: | |
[self showMessage:@"Permission was not granted for Calendar" withTitle:@"Privacy Warning"]; | |
break; | |
default: | |
break; | |
} | |
} | |
- (void) requestCalendarAccess | |
{ | |
__weak typeof (self) weakSelf = self; | |
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { | |
if (granted) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[weakSelf provideCalendarAccess]; | |
}); | |
} else { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[weakSelf showMessage:@"Something went wrong. Try again." withTitle:@"Calendar Error"]; | |
NSLog(@"calendar error : %@", error); | |
}); | |
} | |
}]; | |
} | |
- (void) provideCalendarAccess | |
{ | |
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init]; | |
EKEvent *event = [EKEvent eventWithEventStore:self.eventStore]; | |
event.title = self.session.name; | |
event.location = self.session.location.name; | |
event.startDate = self.session.startDate; | |
event.endDate = self.session.endDate; | |
addController.event = event; | |
addController.eventStore = self.eventStore; | |
addController.editViewDelegate = self; | |
[self.presentingViewController presentViewController:addController animated:YES completion:nil]; | |
} | |
- (void) showMessage: (NSString *) message withTitle: (NSString *) title | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message: message | |
delegate:nil | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
[alert show]; | |
} | |
-(instancetype)init | |
{ | |
if (!(self = [super init])) { | |
return nil; | |
} | |
[self setup]; | |
return self; | |
} | |
-(void) setup | |
{ | |
self.eventStore = [[EKEventStore alloc] init]; | |
} | |
#pragma mark - EKEventEditViewDelegate | |
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action | |
{ | |
__weak typeof (self) weakSelf = self; | |
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{ | |
if (action != EKEventEditViewActionCanceled) { | |
[weakSelf showMessage:@"Event saved to calendar!" withTitle:@"Saved"]; | |
} | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment