有没有什么办法iCal事件添加到从自定义应用程序的iPhone日历?
Answer 1:
根据苹果的文档 ,这种情况已经改变有点作为的iOS 6.0。
1)你应该通过请求访问用户的日历“:完成:requestAccessToEntityType”并执行事件块的内部处理。
2)你现在需要提交您的事件或通过“提交”参数您保存/删除通话
其他一切保持不变...
添加EventKit框架和#import <EventKit/EventKit.h>
你的代码。
在我的例子,我有一个的NSString * savedEventId实例属性。
要添加一个事件:
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
self.savedEventId = event.eventIdentifier; //save the event id if you want to access this later
}];
删除事件:
EKEventStore* store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent* eventToRemove = [store eventWithIdentifier:self.savedEventId];
if (eventToRemove) {
NSError* error = nil;
[store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
}
}];
这增加了事件默认日历,如果您有多个日历,那么你就必须找出哪一个是
斯威夫特版本
您需要导入EventKit框架
import EventKit
添加事件
let store = EKEventStore()
store.requestAccessToEntityType(.Event) {(granted, error) in
if !granted { return }
var event = EKEvent(eventStore: store)
event.title = "Event Title"
event.startDate = NSDate() //today
event.endDate = event.startDate.dateByAddingTimeInterval(60*60) //1 hour long meeting
event.calendar = store.defaultCalendarForNewEvents
do {
try store.saveEvent(event, span: .ThisEvent, commit: true)
self.savedEventId = event.eventIdentifier //save event id to access this particular event later
} catch {
// Display error to user
}
}
删除事件
let store = EKEventStore()
store.requestAccessToEntityType(EKEntityTypeEvent) {(granted, error) in
if !granted { return }
let eventToRemove = store.eventWithIdentifier(self.savedEventId)
if eventToRemove != nil {
do {
try store.removeEvent(eventToRemove, span: .ThisEvent, commit: true)
} catch {
// Display error to user
}
}
}
Answer 2:
您可以使用OS 4.0事件Kit框架做到这一点。
右键点击该组和文件导航框架组在窗口的左侧。 选择“添加”,然后“现有框架”,然后“EventKit.Framework”。
然后,你应该能够使用如下代码添加事件:
#import "EventTestViewController.h"
#import <EventKit/EventKit.h>
@implementation EventTestViewController
- (void)viewDidLoad {
[super viewDidLoad];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"EVENT TITLE";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
@end
Answer 3:
是的,有仍然对此没有API(2.1)。 但是,这似乎是在WWDC有很多人都在功能已有意(包括我自己)和推荐是去下面的网站,并为此创建功能请求。 如果有足够的兴趣,他们可能最终移动ICal.framework公众SDK。
https://developer.apple.com/bugreporter/
Answer 4:
日历访问是在iPhone OS 4.0添加 :
日历访问
应用现在可以创建并直接与事件Kit中的日历应用编辑的事件。
建立经常性的活动,建立开始和结束时间,并将其分配到设备上的任何日历。
Answer 5:
您可以使用事件API像特里斯坦概述添加事件,你也可以添加一个谷歌日历事件,这在iOS日历中显示出来。
使用谷歌的API的Objective-C客户端
- (void)addAnEvent {
// Make a new event, and show it to the user to edit
GTLCalendarEvent *newEvent = [GTLCalendarEvent object];
newEvent.summary = @"Sample Added Event";
newEvent.descriptionProperty = @"Description of sample added event";
// We'll set the start time to now, and the end time to an hour from now,
// with a reminder 10 minutes before
NSDate *anHourFromNow = [NSDate dateWithTimeIntervalSinceNow:60*60];
GTLDateTime *startDateTime = [GTLDateTime dateTimeWithDate:[NSDate date]
timeZone:[NSTimeZone systemTimeZone]];
GTLDateTime *endDateTime = [GTLDateTime dateTimeWithDate:anHourFromNow
timeZone:[NSTimeZone systemTimeZone]];
newEvent.start = [GTLCalendarEventDateTime object];
newEvent.start.dateTime = startDateTime;
newEvent.end = [GTLCalendarEventDateTime object];
newEvent.end.dateTime = endDateTime;
GTLCalendarEventReminder *reminder = [GTLCalendarEventReminder object];
reminder.minutes = [NSNumber numberWithInteger:10];
reminder.method = @"email";
newEvent.reminders = [GTLCalendarEventReminders object];
newEvent.reminders.overrides = [NSArray arrayWithObject:reminder];
newEvent.reminders.useDefault = [NSNumber numberWithBool:NO];
// Display the event edit dialog
EditEventWindowController *controller = [[[EditEventWindowController alloc] init] autorelease];
[controller runModalForWindow:[self window]
event:newEvent
completionHandler:^(NSInteger returnCode, GTLCalendarEvent *event) {
// Callback
if (returnCode == NSOKButton) {
[self addEvent:event];
}
}];
}
Answer 6:
更新为Dashrath答案迅速4
import UIKit
import EventKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let eventStore = EKEventStore()
eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = "My Event"
event.startDate = Date(timeIntervalSinceNow: TimeInterval())
event.endDate = Date(timeIntervalSinceNow: TimeInterval())
event.notes = "Yeah!!!"
event.calendar = eventStore.defaultCalendarForNewEvents
var event_id = ""
do{
try eventStore.save(event, span: .thisEvent)
event_id = event.eventIdentifier
}
catch let error as NSError {
print("json error: \(error.localizedDescription)")
}
if(event_id != ""){
print("event added !")
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
也不要忘了添加权限使用日历
Answer 7:
雨燕4.0的实现:
通过使用进口在页面的顶部import EventKit
然后
@IBAction func addtoCalendarClicked(sender: AnyObject) {
let eventStore = EKEventStore()
eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(error)")
let event = EKEvent(eventStore: eventStore)
event.title = "Event Title"
event.startDate = Date()
event.endDate = Date()
event.notes = "Event Details Here"
event.calendar = eventStore.defaultCalendarForNewEvents
var event_id = ""
do {
try eventStore.save(event, span: .thisEvent)
event_id = event.eventIdentifier
}
catch let error as NSError {
print("json error: \(error.localizedDescription)")
}
if(event_id != ""){
print("event added !")
}
}
})
}
Answer 8:
简单....使用tapku库....你可以在google这个词,并用它...它的开源...享受.....没必要和这些代码窃听的....
Answer 9:
请记住,在结束日期设置为创建的事件,它是强制性的。
否则会失败(几乎悄无声息)与此错误:
"Error Domain=EKErrorDomain Code=3 "No end date has been set." UserInfo={NSLocalizedDescription=No end date has been set.}"
对我来说,完整的工作代码为:
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *calendarEvent = [EKEvent eventWithEventStore:store];
calendarEvent.title = [NSString stringWithFormat:@"CEmprendedor: %@", _event.name];
calendarEvent.startDate = _event.date;
// 5 hours of duration, we must add the duration of the event to the API
NSDate *endDate = [_event.date dateByAddingTimeInterval:60*60*5];
calendarEvent.endDate = endDate;
calendarEvent.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:calendarEvent span:EKSpanThisEvent commit:YES error:&err];
self.savedEventId = calendarEvent.eventIdentifier; //saving the calendar event id to possibly deleted them
}];
Answer 10:
该谷歌的想法是不错的,但有问题。
我可以成功地打开谷歌日历事件屏幕 - 但只有在主桌面版本,它不会对iPhone的Safari正确显示。 谷歌手机日历,这并在Safari上正常显示,似乎并没有与API合作,以添加事件。
就目前而言,我看不出来这其中的一个好办法。
Answer 11:
在斯威夫特工作4.2码
import UIKit
import EventKit
import EventKitUI
class yourViewController: UIViewController{
let eventStore = EKEventStore()
func addEventToCalendar() {
eventStore.requestAccess( to: EKEntityType.event, completion:{(granted, error) in
DispatchQueue.main.async {
if (granted) && (error == nil) {
let event = EKEvent(eventStore: self.eventStore)
event.title = self.headerDescription
event.startDate = self.parse(self.requestDetails.value(forKey: "session_time") as? String ?? "")
event.endDate = self.parse(self.requestDetails.value(forKey: "session_end_time") as? String ?? "")
let eventController = EKEventEditViewController()
eventController.event = event
eventController.eventStore = self.eventStore
eventController.editViewDelegate = self
self.present(eventController, animated: true, completion: nil)
}
}
})
}
}
现在,我们将得到事件的屏幕,在这里你还可以修改您的设置:
现在添加委托方法来处理取消和添加事件屏幕的情况下,按钮操作:
extension viewController: EKEventEditViewDelegate {
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
controller.dismiss(animated: true, completion: nil)
}
}
注意:不要忘了添加NSCalendarsUsageDescription钥匙插入信息的plist。