是否可以在xCode中的Objective-C中打开本机iPhone“添加事件”日历提示,其中已填写了几个字段?例如姓名,地址和开始/结束日期?如果是这样,怎么样? 这将允许用户仍然改变一些参数:他什么时候想要被警告等等。
我环顾四周,但我发现的是在没有用户确认的情况下自动添加事件的方法。
答案 0 :(得分:1)
第1步 首先采取日历权限
dispatch_async(dispatch_get_main_queue(), ^{
[self.eventManager.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(granted==NO)
{
BOOL permission=[[NSUserDefaults standardUserDefaults] boolForKey:@"CalendarPermissionAlert"];
if(permission==NO) {
kAppDelegateObject.eventManager.eventsAccessGranted=NO;
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"CalendarPermissionAlert"];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Calendar Access is OFF"
message:kCalenderResetMessage
delegate:self
cancelButtonTitle:@"CANCEL"
otherButtonTitles:@"SETTING",nil];
[alert show];
alert.tag=101;
return ;
}
}
第2步
//添加活动
-(void)addEventWithMessage:(NSString*)eventMessage withEventDate:(NSDate *)eventDate
EKEventStore *eventStore;
eventStore = [[EKEventStore alloc] init];
//创建一个新的事件对象。 EKEvent * event = [EKEvent eventWithEventStore:eventStore];
// Set the event title.
event.title = eventMessage;
// Set its calendar.
NSString *identifier=[[NSUserDefaults standardUserDefaults]objectForKey:@"calenderId"]; //your application id
// NSLog(@"cal identifier: %@",identifier);
event.calendar = [eventStore calendarWithIdentifier:identifier];
//set Alarm
NSTimeInterval secondsInOneHours = 1 * 60 * 60;
NSDate *dateOneHoursAhead = [eventDate dateByAddingTimeInterval:secondsInOneHours];
// Set the start and end dates to the event.
event.startDate = eventDate;
event.endDate = dateOneHoursAhead; //
NSError *error;
if ([eventStore saveEvent:event span:EKSpanFutureEvents commit:YES error:&error]) {
// NSLog(@"Event Added");
}
else{
// An error occurred, so log the error description.
// NSLog(@"%@", [error localizedDescription]);
}
答案 1 :(得分:0)
以下是我使用EKEventEditViewController进行处理的方法!
首先:
@import EventKitUI;
位于.m文件的最顶层。
然后设置EKEventEditViewDelegate
然后,当您要添加事件时,请使用以下方法:
- (IBAction)addToCalendar:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
NSLog(@"%@", error);
// display error message here
}
else if (!granted)
{
NSLog(@"%@ acce sdenied", error);
// display access denied error message here
}
else
{
EKEvent *event = [EKEvent eventWithEventStore: eventStore];
event.title = nom;
event.location = adresse;
// Set the start and end dates to the event.
event.startDate = startDate;
event.endDate = endDate; //
EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
eventViewController.event = event;
eventViewController.eventStore=eventStore;
eventViewController.editViewDelegate = self;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[eventViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:eventViewController animated:YES completion:NULL];
}
});
}];
}
}
最后,添加此委托方法来处理完成操作:
-(void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
NSError *error;
switch (action) {
case EKEventEditViewActionCancelled:
// User tapped "cancel"
NSLog(@"Canceled");
break;
case EKEventEditViewActionSaved:
NSLog(@"Saved");
[controller.eventStore saveEvent:controller.event span: EKSpanFutureEvents error:&error];
[calendarBouton setTitle:@"Ajouté!" forState:UIControlStateDisabled];
calendarBouton.enabled = NO;
break;
case EKEventEditViewActionDeleted:
// User tapped "delete"
NSLog(@"Deleted");
break;
default:
NSLog(@"Default");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}