requestAccessToEntity iOS6-向后兼容性 - EKEventStore(r

2019-08-01 01:31发布

以下iOS6的eventKit和新的隐私设置,我使用下面的代码 - 这工作完全正常的iOS6的设备。

不过,我想同样的代码也工作了与iOS 5.x的设备,我希望不是写了“相同的代码”两次 - 好像不对。

在一个优雅的解决任何人都可以帮助?

 EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

// some code 


}];

Answer 1:

我使用的是这样的:

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();
}

我认为,应该减少代码的重复。



Answer 2:

如果你告诉一个EKEventEditViewController在modalviewcontroller时,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];
}


Answer 3:

我有与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)。



文章来源: requestAccessToEntity iOS6- Backwards compatibility - EKEventStore