RequestAccessToentity IOS6-向后兼容 - EkeventStore
-
11-12-2019 - |
题
遵循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();
}
.
我认为应该减少代码复制。
其他提示
如果您将ModalViewController的活动显示为EkeventedItViewController,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];
}
. 我有一个hevenutil.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
}
.
和在我想要访问日历的视图控制器中,我导入revileutil.h文件并调用此功能:
[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];
.
displaymodifycalendarertview是我想要调用的函数如果给出了日历权限(ios6或ios <6)。
不隶属于 StackOverflow