ios defaultCalendarForNewEvents 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12454324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
defaultCalendarForNewEvents failed
提问by Alex.BIG
When I try to call [newEventStore defaultCalendarForNewEvents], it returns an error message says:
当我尝试调用 [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.}
The app runs for a long time. The problem came to me first time. The phone runs IOS6 Beta4. model is iphone 4. Is there anyone knows when the defaultCalendarForNewEvents method will get failed? I can not get any useful information from googling.
该应用程序运行了很长时间。这个问题第一次出现在我身上。手机运行IOS6 Beta4。型号是 iphone 4。有谁知道 defaultCalendarForNewEvents 方法什么时候会失败?我无法从谷歌搜索中获得任何有用的信息。
回答by yunas
On iOS6, Apple introduced a new privacy control that allows the user to control the accessibility of contacts and calenders for each app. So, on the code side, you need to add some way to request the permission. In iOS5 or before, we can always call
在 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];
}
回答by Alphonse R. Dsouza
Ios app wont be able to get any data from the Calendar on the iOS6 system if you don't call the – requestAccessToEntityType:completion:function to prompt a dialog to ask your users to grant access to your app to access the Calendar/Reminder . Code will look like:
如果您不调用– requestAccessToEntityType:completion:函数来提示对话框,要求您的用户授予对您的应用程序的访问权限以访问 Calendar/Reminder ,则Ios 应用程序将无法从 iOS6 系统上的 Calendar 获取任何数据。代码将如下所示:
//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.
}
// Below is a block for checking is current ios version higher than required version.
// 下面是用于检查当前 ios 版本是否高于所需版本的块。
- (BOOL)checkIsDeviceVersionHigherThanRequiredVersion:(NSString *)requiredVersion
{
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:requiredVersion options:NSNumericSearch] != NSOrderedAscending)
{
return YES;
}
return NO;
}
回答by Vaibhav Saran
iOS6+
requires users authentication to save event to his device calendar.
Here is a code snippt:
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);
}
And check my this post
if you are facing trouble in setting alarm to the application.
并检查我this post
,如果你正面临麻烦设置报警到应用程序。
回答by Alexander Zimin
I had the same problem, but finally find what was the reason.
我遇到了同样的问题,但终于找到了原因。
My case was to add my Reminder and Calendar events, but I was using one EKEventStore
. In the end I seperate them and problem dissapeared:
我的情况是添加我的提醒和日历事件,但我使用了一个EKEventStore
. 最后我把它们分开了,问题消失了:
private static let calendarEventStore = EKEventStore()
private static let remindersEventStore = EKEventStore()
So now I'm using calendarEventStore
for all things related to calendar event and remindersEventStore
for reminder one.
所以现在我calendarEventStore
用于与日历事件和remindersEventStore
提醒一相关的所有事情。
——
——
In my opinion it was related to the fact that I requested defaultCalendarForNewEvents
and defaultCalendarForNewReminders()
in one EKEventStore
entity.
在我看来,这是有关我所要求的事实defaultCalendarForNewEvents
,并defaultCalendarForNewReminders()
在一个EKEventStore
实体。
Also this one from EKEventStore
docs:
还有这个来自EKEventStore
文档:
Events, Reminders, and Calendar objects retrieved from an event store cannot be used with any other event store
从事件存储检索的事件、提醒和日历对象不能与任何其他事件存储一起使用
回答by Alex.BIG
Solved. I accidentally turn off the app access to calendar in Setting->Privacy on IOS6
解决了。我不小心在IOS6的设置->隐私中关闭了应用程序对日历的访问
回答by Maxim Shoustin
Swift form
迅捷形式
(based on @yunas answer)
(基于@yunas 的回答)
_estore = EKEventStore()
_estore.reset()
_estore.requestAccessToEntityType(EKEntityTypeEvent) { (granted, error) in
if granted {
println("allowed")
/* ... */
} else {
println("not allowed")
}
}
It will open "Access" pop-up
它将打开“访问”弹出窗口
回答by Stefan van de Laarschot
For Xamarin users:
对于 Xamarin 用户:
EKEventStore eventStore = new EKEventStore();
eventStore.RequestAccess(EKEntityType.Event, (bool granted, NSError e) =>
{
if (granted)
{
//Your code here when user gief permissions
}
});
回答by Warewolf
Go to Simulator/Device settings/Privacy/Calendars/YourApp Tap ON to allow calendar access. And retry your code.It will work.
转到模拟器/设备设置/隐私/日历/你的应用程序 点击打开以允许访问日历。并重试您的代码。它会起作用。