我的应用程序的一部分需要日历访问,这需要调用EKEventStore
method -(void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion
从 iOS 7 开始。
我添加了请求,如果用户选择允许访问,一切都会顺利进行,但如果用户拒绝或之前拒绝访问,则会出现问题。我添加了一个UIAlertView
如果访问被拒绝,则通知用户,但是UIAlertView
始终需要 20-30 秒才能出现,并在此期间完全禁用 UI。调试表明[alertView show]
在延迟之前运行,即使它实际上直到延迟之后才显示。
为什么会发生这种延迟以及如何消除它?
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
if (granted) {
[self createCalendarEvent];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Calendar Access Denied"
message:@"Please enable access in Privacy Settings to use this feature."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}];
[alertView show]
不是线程安全的,因此它将其 UI 更改添加到调度完成块的队列而不是主队列。我通过添加解决了这个问题dispatch_async(dispatch_get_main_queue(), ^{});
完成块内的代码周围:
[eventStore requestAccessToEntityType:EKEntityTypeEvent
completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
[self createCalendarEvent];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Calendar Access Denied"
message:@"Please enable access in Privacy Settings to use this feature."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
});
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)