32

当我尝试调用 [newEventStore defaultCalendarForNewEvents] 时,它返回一条错误消息:

[707:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
[707:907] Error!Failed to save appointment. Description:Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1fc679f0 {NSLocalizedDescription=No calendar has been set.}

该应用程序运行很长时间。问题第一次来找我。手机运行IOS6 Beta4。型号是 iphone 4。有谁知道 defaultCalendarForNewEvents 方法何时会失败?我无法从谷歌搜索中获得任何有用的信息。

4

8 回答 8

55

在 iOS6 上,Apple 引入了新的隐私控制,允许用户控制每个应用程序的联系人和日历的可访问性。因此,在代码方面,您需要添加一些方法来请求权限。在iOS5或之前,我们可以随时调用

EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
    // iOS 6 and later
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
            // code here for when the user allows your app to access the calendar
            [self performCalendarActivity:eventStore];
        } else {
            // code here for when the user does NOT allow your app to access the calendar
        }
    }];
} else {
    // code here for iOS < 6.0
    [self performCalendarActivity:eventStore];
}
于 2012-11-19T10:07:06.140 回答
14

如果您不调用 –requestAccessToEntityType :completion: 函数来提示对话框要求您的用户授予对您的应用程序的访问权限以访问日历/提醒,则 iOS 应用程序将无法从 iOS6 系统上的日历中获取任何数据。代码将如下所示:

//CalendarEventHandler.h  
@interface CalendarEventHandler : NSObject
{
EKEventStore *eventStore;
}
@property (nonatomic, strong) EKEventStore *eventStore;


//CalendarEventHandler.m 
self.eventStore =[[EKEventStore alloc] init];
if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
        [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            if (granted){
                //---- codes here when user allow your app to access theirs' calendar.

            }else
            {
                //----- codes here when user NOT allow your app to access the calendar.  
            }  
        }];

    }else {
                //---- codes here for IOS < 6.0.

    }

// 下面是检查当前 ios 版本是否高于所需版本的块。

 - (BOOL)checkIsDeviceVersionHigherThanRequiredVersion:(NSString *)requiredVersion
 {
  NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

   if ([currSysVer compare:requiredVersion options:NSNumericSearch] != NSOrderedAscending)
   {
       return YES;
   }

       return NO;
  }
于 2012-09-25T14:52:17.677 回答
7

iOS6+需要用户身份验证才能将事件保存到他的设备日历。这是一个代码片段:

    // save to iphone calendar
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // iOS 6 and later
        // This line asks user's permission to access his calendar
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
        {
            if (granted) // user user is ok with it
            {
                EKEvent *event = [EKEvent eventWithEventStore:eventStore];
                event.title  = aTitle;
                event.allDay = YES;

                NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
                [dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
                event.startDate = event.endDate = //put here if start and end dates are same

                [event setCalendar:[eventStore defaultCalendarForNewEvents]];
                NSError *err;

                [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

                if(err)
                    NSLog(@"unable to save event to the calendar!: Error= %@", err);

            }
            else // if he does not allow 
            {
                [[[UIAlertView alloc]initWithTitle:nil message:alertTitle delegate:nil cancelButtonTitle:NSLocalizedString(@"plzAlowCalendar", nil)  otherButtonTitles: nil] show];
                return;
            }
        }];
    }

    // iOS < 6
    else
    {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title  = aTitle;
        event.allDay = YES;

        NSDateFormatter *dateFormat = [[UIApplicationSingleton sharedManager] aDateFormatter];
        [dateFormat setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
        event.startDate = event.endDate = //put here if start and end dates are same

        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;

        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

        if(err)
            NSLog(@"unable to save event to the calendar!: Error= %@", err);

    }

this post如果您在为应用程序设置警报时遇到问题,请检查我的。

于 2013-04-25T11:41:00.633 回答
3

解决了。我不小心在 IOS6 的设置->隐私中关闭了应用程序对日历的访问权限

于 2012-09-17T08:18:25.850 回答
3

我遇到了同样的问题,但终于找到了原因。

我的情况是添加我的提醒和日历事件,但我使用的是一个EKEventStore. 最后我将它们分开并且问题消失了:

private static let calendarEventStore = EKEventStore()
private static let remindersEventStore = EKEventStore()

所以现在我正在使用calendarEventStore与日历事件和remindersEventStore提醒相关的所有事情。

——</p>

在我看来,这与我defaultCalendarForNewEventsdefaultCalendarForNewReminders()一个EKEventStore实体中提出的请求有关。

这也是来自EKEventStore文档的:

从事件存储中检索的事件、提醒和日历对象不能与任何其他事件存储一起使用

于 2017-02-26T22:53:52.040 回答
1

迅捷形式

(基于@yunas 的回答)

_estore = EKEventStore()
_estore.reset()

_estore.requestAccessToEntityType(EKEntityTypeEvent) { (granted, error) in
    if granted {
        println("allowed")
        /* ... */
    } else {
        println("not allowed")
    }           
}

它将打开“访问”弹出窗口

于 2014-09-14T10:28:07.027 回答
0

转到模拟器/设备设置/隐私/日历/YourApp 点击打开以允许访问日历。并重试您的代码。它会工作。

于 2013-02-05T11:26:03.380 回答
0

对于 Xamarin 用户:

EKEventStore eventStore = new EKEventStore();
eventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError e) =>
{
    if (granted)
    {
       //Your code here when user gief permissions
    }
});
于 2015-10-26T15:35:08.043 回答