From 06c626ed66cc55831ea549cc28388714116e976d Mon Sep 17 00:00:00 2001 From: Maciej Banasiewicz Date: Tue, 6 Jan 2015 19:40:19 +0100 Subject: [PATCH] Update ViewController.m --- EventTest/ViewController.m | 71 ++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/EventTest/ViewController.m b/EventTest/ViewController.m index 919cc0a..77f3a57 100644 --- a/EventTest/ViewController.m +++ b/EventTest/ViewController.m @@ -4,18 +4,20 @@ @interface ViewController () @end -@implementation ViewController +@implementation ViewController { + EKEventStore* eventStore; +} - (void)viewDidLoad { [super viewDidLoad]; - static EKEventStore* eventStore; if (!eventStore) { - eventStore = [[EKEventStore alloc] init]; + eventStore = [EKEventStore new]; } - NSArray* cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; - NSLog(@"1st time calendars @%", cals); + // Tu wywaliłem + // NSArray* cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; + // NSLog(@"1st time calendars %@", cals); EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; @@ -25,36 +27,26 @@ - (void)viewDidLoad { case EKAuthorizationStatusAuthorized: { NSLog(@"permission was already granted before"); - NSArray* cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; +// NSArray *cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; + [self pullEvents]; break; } case EKAuthorizationStatusNotDetermined: { - [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) - { - if (granted) - { - NSLog(@"granted after user confirmation"); - dispatch_sync(dispatch_get_main_queue(), ^{ - [eventStore reset]; - // refetch calendars - NSArray* cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; - NSLog(@"calendars @%", cals); - }); - } else { - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" message:@"Permission was not granted for Calendar" - delegate:nil - cancelButtonTitle:@"OK" - otherButtonTitles:nil]; - [alert show]; - } + __weak typeof(self) weakSelf = self; + [eventStore requestAccessToEntityType:EKEntityTypeEvent + completion:^(BOOL granted, NSError *error) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [weakSelf calendarAccessGranted:granted]; + }]; }]; } break; case EKAuthorizationStatusDenied: case EKAuthorizationStatusRestricted: { - UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" message:@"Permission was not granted for Calendar" + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" + message:@"Permission was not granted for Calendar" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; @@ -66,6 +58,35 @@ - (void)viewDidLoad { } } +- (void)calendarAccessGranted:(BOOL)granted { + if (granted) { + [self pullEvents]; + } else { + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Warning" + message:@"Permission was not granted for Calendar" + delegate:nil + cancelButtonTitle:@"OK" + otherButtonTitles:nil]; + [alert show]; + } + +} + +// Na moim symulatorze działa i zwraca kalendarze +// Mam nadzieję że pomoże +- (void)pullEvents { +// NSLog(@"granted after user confirmation"); + + NSArray* cals = [eventStore calendarsForEntityType:EKEntityTypeEvent]; + +// dispatch_sync(dispatch_get_main_queue(), ^{ +// [eventStore reset]; +// // refetch calendars +// +// NSLog(@"calendars %@", cals); +// }); +} + - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];