Ios 使用requestAccessToEntityType时出现错误的访问错误
当我尝试运行此代码时,我收到一个EXEC\u BAD\u访问错误,并且用户不允许访问日历。requestAccessToEntityType是否在单独的线程上运行,如果是这种情况,如何访问主线程以显示UIAlertViewIos 使用requestAccessToEntityType时出现错误的访问错误,ios,objective-c,Ios,Objective C,当我尝试运行此代码时,我收到一个EXEC\u BAD\u访问错误,并且用户不允许访问日历。requestAccessToEntityType是否在单独的线程上运行,如果是这种情况,如何访问主线程以显示UIAlertView EKEventStore *store = [[EKEventStore alloc] init]; if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
EKEventStore *store = [[EKEventStore alloc] init];
if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
[self readEvents];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Denied Access To Calendar"
message:@"Access was denied to the calendar, please go into settings and allow this app access to the calendar!"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil,
nil];
[alert show];
}
}];
}
根据
当用户点击以授予或拒绝访问时,完成处理程序
将在任意队列上调用
因此,是的,它可能位于与UI线程不同的线程上。您只能从主GUI线程发出警报
查看
performselectornmainthread
。此处的更多信息:您的应用程序崩溃的原因是您试图处理GUI元素,即后台线程中的UIAlertView,您需要在主线程上运行它或尝试使用调度队列
使用调度队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
//show your UIAlertView here... or any GUI stuff
});
或者您可以在主线程上显示GUI元素,如下所示
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
您可以在此上获得有关在线程上使用GUI元素的更多详细信息您可以发布崩溃日志吗?这可能是因为您的UI元素正在后台线程或选择器中更新。为了让您知道在主线程中运行警报,您可以使用[alert performSelectorOnMainThread:@selector(show)];