requestAccessToEntity iOS6-向后兼容性 - EKEventStore
问题描述:
下面的iOS6 eventKit和新的隐私设置我使用下面的代码 - 这在iOS6设备上工作得很好。requestAccessToEntity iOS6-向后兼容性 - EKEventStore
不过,我想要相同的代码也适用于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];
}
+0
我有这个问题,它不等待,直到它完成,所以我得到null在我所有事件的数组中,我不能使用我想要的地方和3秒后它得到打印我的所有事件数组如何过来这个 –
答
我有
+(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.m文件
而在我想要访问日历的视图控制器中,我导入EventUtil.h文件并调用此函数:
[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];
displayModifyCalendarAlertView是我想如果日历权限被赋予(无论是iOS6的或iOS < 6)来调用函数。
嗯,不是最优雅的解决方案,但我可以忍受它,谢谢。 – chewy