0

我正在做一个涉及 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];
    }
}

我的错误信息相当模糊,但我猜它是关于不能被解雇的主要威胁的东西?

我不确定。但是,我还想在未设置权限时获得“此应用程序需要您的权限”对话框,这可能吗?

4

2 回答 2

0

感谢在 Block 语句中分配/显示 UIAlertView

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];
});

我的假设是正确的,它需要在主线程中调度

于 2013-05-02T10:24:15.747 回答
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];
        });
于 2013-05-02T10:25:37.430 回答