当我尝试调用[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方法什么时候会失败?我无法通过谷歌搜索获得任何有用的信息。
答案 0 :(得分:54)
在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];
}
答案 1 :(得分:14)
//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;
}
答案 2 :(得分:7)
iOS6+
要求用户进行身份验证,以将事件保存到其设备日历中。
这是代码snippt:
// 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
。
答案 3 :(得分:3)
解决。 我在“设置 - &gt; IOS6隐私”
中意外关闭了对日历的应用访问权限答案 4 :(得分:3)
我遇到了同样的问题,但终于找到了原因。
我的情况是添加我的提醒和日历事件,但我使用了一个EKEventStore
。最后,我将它们分开,问题消失了:
private static let calendarEventStore = EKEventStore()
private static let remindersEventStore = EKEventStore()
现在我正在使用calendarEventStore
来处理与日历事件相关的所有事情,并使用remindersEventStore
来提醒第一个事件。
-
在我看来,这与我在一个defaultCalendarForNewEvents
实体中请求defaultCalendarForNewReminders()
和EKEventStore
的事实有关。
此问题来自EKEventStore
docs:
从事件存储中检索的事件,提醒和日历对象 不能与任何其他活动商店一起使用
答案 5 :(得分:1)
(基于@yunas回答)
_estore = EKEventStore()
_estore.reset()
_estore.requestAccessToEntityType(EKEntityTypeEvent) { (granted, error) in
if granted {
println("allowed")
/* ... */
} else {
println("not allowed")
}
}
它将打开&#34; Access&#34;弹出
答案 6 :(得分:0)
转到模拟器/设备设置/隐私/日历/ YourApp点击开以允许日历访问。 并重试您的代码。它会工作。
答案 7 :(得分:0)
对于Xamarin用户:
EKEventStore eventStore = new EKEventStore();
eventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError e) =>
{
if (granted)
{
//Your code here when user gief permissions
}
});