简体   繁体   English

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

[英]unable to add event using EventKit in iOS app

I am trying to add event in iOS app using Event kit with following code:我正在尝试使用带有以下代码的事件工具包在 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];
    });
}];

But I am getting granted as false so it doesn't add the event.但是我被授予错误,所以它不会添加事件。 It is not working in my simulator and device both, even after I reset my simulator.即使在我重置模拟器后,它也无法在我的模拟器和设备中运行。 Same code works in another app of mine.相同的代码适用于我的另一个应用程序。 Can someone please suggest me what I should do?有人可以建议我应该做什么吗?
Edit: Error is always nil编辑:错误始终为零

When app launched for first time, a message asking for permissions will be shown to the user, and app will be able to access the calendar if only the user granted permission.应用程序第一次启动时,会向用户显示一条请求权限的消息,只有用户授予权限,应用程序才能访问日历。 In your case you have declined permission for calendar.在您的情况下,您拒绝了日历的许可。

For fixing this issue, Go to Settings -> -> Allow to access calendar = YES.要解决此问题,请转到设置 -> -> 允许访问日历 = YES。

Seems like you're requesting to get the access every time.似乎您每次都要求获得访问权限。 You should better check the authorisation status first and if it's not determined, only then request for the authorisation.您最好先检查授权状态,如果不能确定,则再请求授权。 First check this using "authorizationStatusForEntityType" and create a new method to save the event, not inside "requestAccessToEntityType".首先使用“authorizationStatusForEntityType”检查并创建一个新方法来保存事件,而不是在“requestAccessToEntityType”中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM