OGeek|极客世界-中国程序员成长平台

标题: ios - 无法在 iOS 应用中使用 EventKit 添加事件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:21
标题: ios - 无法在 iOS 应用中使用 EventKit 添加事件

我正在尝试使用带有以下代码的事件工具包在 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 所以它不会添加事件。即使我重置了我的模拟器,它也不能在我的模拟器和设备中工作。相同的代码适用于我的另一个应用程序。有人可以建议我该怎么做吗?
编辑:错误始终为零



Best Answer-推荐答案


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

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

关于ios - 无法在 iOS 应用中使用 EventKit 添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35988524/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4