无法在iOS应用程序中使用EventKit添加事件

无法在iOS应用程序中使用EventKit添加事件,ios,eventkit,Ios,Eventkit,我正在尝试使用事件工具包在iOS应用程序中添加事件,代码如下: EKEventStore *store = [[EKEventStore alloc] init]; [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (!granted) { return; } dispatch_async(dispatch_get_main_queu

我正在尝试使用事件工具包在iOS应用程序中添加事件,代码如下:

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    dispatch_async(dispatch_get_main_queue(), ^{
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = eventName;
        event.startDate = [NSDate dateFromString:[NSString stringWithFormat:@"%@ %@",[arrEvent objectAtIndex:1],[arrEvent objectAtIndex:2]] withFormat:@"MM/dd/yyyy HH:mm"];
        event.endDate = [NSDate dateFromString:[NSString stringWithFormat:@"%@ %@",[arrEvent objectAtIndex:1],[arrEvent objectAtIndex:3]] withFormat:@"MM/dd/yyyy HH:mm"];
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

        NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
        NSLog(@"%@",savedEventId);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event added to calendar" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"View Calendar", nil];
        alert.tag = 101;
        [alert show];
    });
}];
但我被授予为false,因此它不会添加事件。它在我的模拟器和设备中都不工作,即使我重置了模拟器。同样的代码在我的另一个应用程序中有效。有人能建议我该怎么做吗?

编辑:错误始终为零

当应用程序首次启动时,将向用户显示请求权限的消息,并且只有用户授予权限,应用程序才能访问日历。在您的情况下,您已拒绝日历的权限


要解决此问题,请转到设置->->允许访问日历=是。

似乎您每次都在请求访问。您最好先检查授权状态,如果未确定,则仅请求授权。首先使用“authorizationStatusForEntityType”检查此项,并创建一个新方法来保存事件,而不是在“requestAccessToEntityType”中。如果
已授予
,则记录
错误
以查看原因。我没有看到错误,并且已授予是否。我也没有收到接受或拒绝访问日历的警报