简体   繁体   English

requestAccessToEntity iOS6-向后兼容性 - EKEventStore

[英]requestAccessToEntity iOS6- Backwards compatibility - EKEventStore

following iOS6 eventKit and the new privacy settings I am using the following code - which works perfectly fine on iOS6 devices. 跟随iOS6 eventKit和新的隐私设置我使用以下代码 - 这在iOS6设备上运行得非常好。

Still, I would like the same code to work also for devices with iOS 5.x and I wish not to write a the "same code" twice - Seems wrong. 不过,我希望相同的代码也适用于iOS 5.x的设备,我希望不要两次写“相同的代码” - 似乎错了。

Can anyone assist in an elegant solution ? 任何人都可以协助优雅的解决方案

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

// some code 


}];

I'm using this: 我正在使用这个:

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

I think that should reduce code duplication. 我认为这应该减少代码重复。

If you show the event in a modalviewcontroller with a EKEventEditViewController, iOS automatically show you a view saying what to do if the permission is denied. 如果您在带有EKEventEditViewController的modalviewcontroller中显示事件,iOS会自动显示一个视图,说明如果权限被拒绝该怎么办。 So, I do this: 所以,我这样做:

In viewDidLoad: 在viewDidLoad中:

eventStore = [[EKEventStore alloc] init];

In the method used to add the event: 在用于添加事件的方法中:

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];
}

I have an EventUtil.m file with 我有一个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
}

And in the view controller that I want to access the calendar I import the EventUtil.h file and call this function: 在我想要访问日历的视图控制器中,我导入EventUtil.h文件并调用此函数:

[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];

displayModifyCalendarAlertView is the function I want to call if the calendar permission is given (either iOS6 or iOS < 6). 如果给出了日历权限(iOS6或iOS <6),则displayModifyCalendarAlertView是我想要调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM