iOS - 允许用户添加填写了某些字段的日历事件
iOS - allow user to add Calendar Event with some fields filled in
是否可以在 xCode 中的 Objective-C 中打开本机 iPhone "Add Event" 日历提示,其中已经填写了几个字段?例如姓名、地址和 start/end 日期?如果是这样,如何?
这将允许用户仍然更改几个参数:他想什么时候收到警报等。
我环顾四周,但我只找到了无需用户确认即可自动添加事件的方法。
步骤 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 *事件 = [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]);
}
这是我处理它的方式...使用 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];
}
是否可以在 xCode 中的 Objective-C 中打开本机 iPhone "Add Event" 日历提示,其中已经填写了几个字段?例如姓名、地址和 start/end 日期?如果是这样,如何? 这将允许用户仍然更改几个参数:他想什么时候收到警报等。
我环顾四周,但我只找到了无需用户确认即可自动添加事件的方法。
步骤 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 *事件 = [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]);
}
这是我处理它的方式...使用 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];
}