gpt4 book ai didi

iphone - eventkit 并从应用程序将事件添加到日历中

转载 作者:行者123 更新时间:2023-11-28 20:40:01 25 4
gpt4 key购买 nike

我想问我需要从我的应用程序添加事件,我从日期选择器中选择日期并在文本框中写入事件,它在日历中命中,iphone 的默认日历我怎么能这样做是使用框架eventkit

我用了这个代码但是没有用

- (void)viewDidLoad
{
self.eventStore = [[EKEventStore alloc] init];

self.eventsList = [[NSMutableArray alloc] initWithArray:0];

// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];

// create an Add button
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bell.png" ] style:UIBarButtonItemStylePlain target:self action:@selector(addEvent:)];
//UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(addEvent:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];

[super viewDidLoad];
}

-(IBAction) addEvent:(id)sender
{
self.eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
// event.title =@"Whatever you want your title to be";
event.title = @"a";

// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

event.allDay = YES;

EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];
addController.event = event;
addController.eventStore = self.eventStore;
[self presentModalViewController:addController animated:YES];

addController.editViewDelegate = self;
[addController release];
}

最佳答案

首先在头文件中导入 EventKitUI/EventKitUI.h、EventKit/EventKit.h 框架。这是将事件添加到默认 iCal(iPhone 日历)的代码。

-(IBAction) addEvent:(id)sender 
{
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *events = [EKEvent eventWithEventStore:eventStore];

events.title = @"Title";
events.notes = @"Description";
events.location = @"Location";
events.startDate = [NSDate date];
events.endDate = [NSDate date];
events.availability = EKEventAvailabilityFree;

[events setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:events span:EKSpanThisEvent error:&err];
NSLog(@"Error From iCal : %@", [err description]);
}

然后使用这段代码在EKEventEditViewController中查看保存的事件,

EKEventEditViewController *editViewController = [[EKEventEditViewController alloc] init];
editViewController.editViewDelegate = self;
editViewController.event = event3;
editViewController.eventStore = eventStore1;
[self presentModalViewController:editViewController animated:YES];

我已经使用了这些代码并且它对我有用。谢谢。

关于iphone - eventkit 并从应用程序将事件添加到日历中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8998904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com