ProcessAccessSEENTITY IOS6 - обратная совместимость - EkeventStore
-
11-12-2019 - |
Вопрос
После IOS6 EventKit и новые настройки конфиденциальности я использую следующий код - который отлично работает на устройствах IOS6.
Тем не менее, я бы хотел, чтобы тот же код был работать также для устройств с iOS 5.x, и я хочу не писать «тот же код» дважды - кажется неверным.
Может кто-нибудь помочь в
. EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// some code
}];
Решение
Я использую это:
void (^addEventBlock)();
addEventBlock = ^
{
NSLog(@"Hi!");
};
EKEventStore *eventStore = [[UpdateManager sharedUpdateManager] eventStore];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
addEventBlock();
}
else
{
NSLog(@"Not granted");
}
}];
}
else
{
addEventBlock();
}
.
Я думаю, что следует уменьшить дублирование кода.
Другие советы
Если вы покажете событие в ModalViewController с EkeventeditViewController, iOS автоматически показывает вам представление, что делать, если разрешение запрещено.Итак, я делаю это:
в viewdidload:
eventStore = [[EKEventStore alloc] init];
.
в методе, используемом для добавления события:
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
__block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
[weakSelf performSelectorOnMainThread:@selector(addEventToCalendar) withObject:nil waitUntilDone:YES];
}
else
{
NSLog(@"Not granted");
}
}];
}
else
{
[self addEventToCalendar];
}
. У меня есть файл eventutil.m с
+(void)proxyForIOS6EventKitToCallFunction:(SEL)function WithViewController:(UIViewController*)viewController {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if([app.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// For iOS 6
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
hud.labelText = @"";
//invoke requestAccessToEntityType...
[app.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
//Handle the response here…
//Note: If you prompt the user, make sure to call the main thread
if (granted == YES) {
dispatch_async(dispatch_get_main_queue(), ^{
[viewController performSelector:function];
});
}
}];
}
else {
[viewController performSelector:function];
}
#pragma clang diagnostic pop
}
.
и в контроллере представления, который я хочу получить доступ к календарю, я импортирую файл eventutil.h и вызовите эту функцию:
[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];
.
DisplayModifyCalendaralertView - это функция, которую я хочу позвонить, если дано разрешение календаря (IOS6 или iOS <6).