草庐IT

objective-c - requestAccessToEntity iOS6-向后兼容-EKEventStore

coder 2023-10-01 原文

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

我认为这应该可以减少代码重复。

关于objective-c - requestAccessToEntity iOS6-向后兼容-EKEventStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12660206/

有关objective-c - requestAccessToEntity iOS6-向后兼容-EKEventStore的更多相关文章

随机推荐