【问题标题】:unable to add event using EventKit in iOS app无法在 iOS 应用中使用 EventKit 添加事件
【发布时间】:2016-03-14 13:10:53
【问题描述】:

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

【问题讨论】:

  • 如果 grantedNO,请登录 error 了解原因。
  • 我没有看到任何错误,并且没有。我也不会收到有关接受或拒绝访问日历的提醒

标签: ios eventkit


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    似乎您每次都要求获得访问权限。您最好先检查授权状态,如果没有确定,请再请求授权。首先使用“authorizationStatusForEntityType”检查并创建一个新方法来保存事件,而不是在“requestAccessToEntityType”中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多