我正在开发一个涉及iOS EventKit的项目。它需要向后兼容ios 5.所以我尝试了在SO上实现的几种方法。当我试图展示UIAlert时,它会崩溃。
-(void)addToCalender {
EKEventStore *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];
[weakSelf performSelectorOnMainThread: @selector(presentEventEditViewControllerWithEventStore:) withObject:eventStore waitUntilDone:YES];
}
else
{
NSLog(@"Not granted");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Announcement"
message: @"It turns out that you are playing Addicus!"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}];
}
else
{
[self performSelectorOnMainThread: @selector(presentEventEditViewControllerWithEventStore:) withObject:eventStore waitUntilDone:NO];
}
}
我的错误信息相当模糊,但我猜它是关于无法解雇的主要问题的事情?
我不确定。但是,如果没有设置权限,我还想获得“此应用程序需要您的权限”对话框,这可能吗?
答案 0 :(得分:0)
感谢Allocating/showing a UIAlertView in a Block statement
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied"
message:@"Een tekst"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease];
[myAlert show];
});
我的假设是对的,需要在主线程
发送答案 1 :(得分:0)
我遇到了同样的问题。我使用了以下代码。请在此处使用您的代码。
dispatch_async(dispatch_get_main_queue(), ^{
});
例如,
dispatch_async(dispatch_get_main_queue(), ^{
// alert code
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Your internet connection is currently unavailable. Please try to connect later." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
[alert show];
[alert release];
});