-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathMGCEventKitSupport.m
205 lines (162 loc) · 6.32 KB
/
MGCEventKitSupport.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// MGCEventKitSupport.m
// Graphical Calendars Library for iOS
//
// Distributed under the MIT License
// Get the latest version from here:
//
// https://github.com/jumartin/Calendar
//
// Copyright (c) 2014-2015 Julien Martin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import <Foundation/Foundation.h>
#import "MGCEventKitSupport.h"
@interface MGCEventKitSupport ()<UIAlertViewDelegate>
@property (nonatomic) EKEvent* savedEvent;
@property (nonatomic, copy) EventSaveCompletionBlockType saveCompletion;
@end
@implementation MGCEventKitSupport
@synthesize eventStore = _eventStore;
// designated initializer
- (instancetype)initWithEventStore:(EKEventStore*)eventStore
{
if (self = [super init]) {
_eventStore = eventStore;
}
return self;
}
- (EKEventStore*)eventStore
{
if (_eventStore == nil) {
_eventStore = [EKEventStore new];
}
return _eventStore;
}
#pragma mark - Calendar access authorization
- (void)checkEventStoreAccessForCalendar:(void (^)(BOOL accessGranted))completion
{
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
switch (status) {
case EKAuthorizationStatusAuthorized:
[self accessGrantedForCalendar];
completion(YES);
break;
case EKAuthorizationStatusNotDetermined:
[self requestCalendarAccess:completion];
break;
case EKAuthorizationStatusDenied:
case EKAuthorizationStatusRestricted:
[self accessDeniedForCalendar];
completion(NO);
}
}
- (void)requestCalendarAccess:(void (^)(BOOL accessGranted))completion
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
MGCEventKitSupport * __weak weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf accessGrantedForCalendar];
completion(YES);
});
}
}];
}
- (void)accessGrantedForCalendar
{
_accessGranted = YES;
}
- (void)accessDeniedForCalendar
{
NSString *title = NSLocalizedString(@"Warning", nil);
NSString *msg = NSLocalizedString(@"Access to the calendar was not authorized", nil);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (void)saveEvent:(EKEvent*)event completion:(void (^)(BOOL saved))completion
{
if (event.hasRecurrenceRules) {
self.savedEvent = event;
self.saveCompletion = completion;
NSString *title = NSLocalizedString(@"This is a repeating event.", nil);
NSString *msg = NSLocalizedString(@"What do you want to modify?", nil);
UIAlertView *sheet = [[UIAlertView alloc]initWithTitle:title message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"This event only", nil), NSLocalizedString(@"All future events", nil), nil];
[sheet show];
}
else {
NSError *error;
BOOL saved = [self.eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (!saved) {
NSLog(@"Error - Could not save event: %@", error.description);
}
if (completion != nil) {
completion(saved);
}
self.saveCompletion = nil;
}
}
#pragma mark - UIAlertViewDelegate
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSAssert(self.savedEvent, @"Saved event is nil");
BOOL saved = NO;
if (buttonIndex != 0) {
EKSpan span = EKSpanThisEvent;
if (buttonIndex == 1) {
span = EKSpanThisEvent;
}
else if (buttonIndex == 2) {
span = EKSpanFutureEvents;
}
NSError *error;
saved = [self.eventStore saveEvent:self.savedEvent span:span commit:YES error:&error];
if (!saved) {
NSLog(@"Error - Could not save event: %@", error.description);
}
}
if (self.saveCompletion != nil) {
self.saveCompletion(saved);
}
self.saveCompletion = nil;
self.savedEvent = nil;
}
@end
@implementation MGCEKEventViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIPopoverPresentationController *popController = self.parentViewController.popoverPresentationController;
BOOL isPopoverPresented = popController && popController.arrowDirection != UIPopoverArrowDirectionUnknown;
// navigation bar is hidden by default when EKEventViewController is presented fullscreen
if (self.presentingViewController && !isPopoverPresented) {
self.navigationController.navigationBarHidden = NO;
// self.navigationController.toolbarHidden = YES;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// this fixes a problem when EKEventViewController is pushed
// that causes a white bar to show on the bottom when returning to the previous view controller
self.navigationController.toolbarHidden = YES;
}
@end