以编程方式在 iPhone 日历中添加自定义事件 [英] Programmatically add custom event in the iPhone Calendar

查看:27
本文介绍了以编程方式在 iPhone 日历中添加自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以将 iCal 事件从自定义 App 添加到 iPhone 日历?

Is there any way to add iCal event to the iPhone Calendar from the custom App?

推荐答案

基于 Apple 文档,从 iOS 6.0 开始,这一点有所改变.

Based on Apple Documentation, this has changed a bit as of iOS 6.0.

1) 您应该通过requestAccessToEntityType:completion:"请求访问用户的日历,并在块内执行事件处理.

1) You should request access to the user's calendar via "requestAccessToEntityType:completion:" and execute the event handling inside of a block.

2) 您需要立即提交事件或将提交"参数传递给您的保存/删除调用

2) You need to commit your event now or pass the "commit" param to your save/remove call

其他一切都保持不变......

Everything else stays the same...

将 EventKit 框架和 #import 添加到您的代码中.

Add the EventKit framework and #import <EventKit/EventKit.h> to your code.

在我的示例中,我有一个 NSString *savedEventId 实例属性.

In my example, I have a NSString *savedEventId instance property.

添加事件:

    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];
        }
    }];

这会将事件添加到您的默认日历中,如果您有多个日历,那么您将找出哪个是

This adds events to your default calendar, if you have multiple calendars then you'll have find out which one that is

Swift 版本

需要导入EventKit框架

You need to import the EventKit framework

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
        }
    }
}

这篇关于以编程方式在 iPhone 日历中添加自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆