Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c requestAccessToEntity iOS6-向后兼容性-EKEventStore_Objective C_Ios_Ios5_Ios6 - Fatal编程技术网

Objective c requestAccessToEntity iOS6-向后兼容性-EKEventStore

Objective c requestAccessToEntity iOS6-向后兼容性-EKEventStore,objective-c,ios,ios5,ios6,Objective C,Ios,Ios5,Ios6,在遵循iOS6 eventKit和新的隐私设置之后,我使用了以下代码——这在iOS6设备上运行得非常好 尽管如此,我还是希望同样的代码也适用于iOS 5.x设备,我不希望再写两次“同样的代码”似乎是错误的 任何人都可以帮助您找到一个优雅的解决方案吗 EKEventStore *eventStore = [[EKEventStore alloc] init]; [eventStore requestAccessToEntityType:EKEntityTypeEvent completi

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

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

如果您在带有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];
}

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

hmm,这不是最优雅的解决方案,但我可以接受,谢谢。我在这方面有问题,它不是等到它完成,所以我在我的所有事件数组中得到空值,我不能在我想要的地方使用,3秒钟后它得到打印我的所有事件数组如何克服这个问题