3

遵循 iOS6 eventKit 和新的隐私设置,我正在使用以下代码 - 在 iOS6 设备上运行良好。

不过,我希望相同的代码也适用于 iOS 5.x 的设备,并且我不希望两次编写“相同的代码” - 似乎是错误的。

任何人都可以提供优雅的解决方案吗?

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

// some code 


}];
4

3 回答 3

9

我正在使用这个:

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

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

于 2012-09-30T11:00:49.193 回答
4

如果您在带有 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];
}
于 2012-10-05T12:05:14.353 回答
0

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

于 2012-11-01T09:59:47.153 回答