以编程方式在 iPhone 日历中添加自定义事件
有没有办法从自定义应用程序将 iCal 事件添加到 iPhone 日历?
Is there any way to add iCal event to the iPhone Calendar from the custom App?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
基于 Apple 文档,从 iOS 6.0 开始,这已经发生了一些变化。
1) 您应该通过“requestAccessToEntityType:completion:”请求访问用户的日历,并在块内执行事件处理。
2) 您需要立即提交事件或将“commit”参数传递给保存/删除调用,
其他一切保持不变...
添加 EventKit 框架和
#import到您的代码。
在我的示例中,我有一个 NSString *savedEventId 实例属性。
添加事件:
删除事件:
这会将事件添加到您的默认日历中,如果您有多个日历,那么您将找出哪个是
Swift 版本
您需要导入 EventKit 框架
添加事件
删除事件
Based on Apple Documentation, this has changed a bit as of iOS 6.0.
1) You should request access to the user's calendar via "requestAccessToEntityType:completion:" and execute the event handling inside of a block.
2) You need to commit your event now or pass the "commit" param to your save/remove call
Everything else stays the same...
Add the EventKit framework and
#import <EventKit/EventKit.h>
to your code.In my example, I have a NSString *savedEventId instance property.
To add an event:
Remove the event:
This adds events to your default calendar, if you have multiple calendars then you'll have find out which one that is
Swift version
You need to import the EventKit framework
Add event
Remove event
您可以使用 OS 4.0 中的 Event Kit 框架来完成此操作。
右键单击窗口左侧“组和文件导航器”中的“FrameWorks”组。 选择“添加”,然后选择“现有框架”,然后选择“EventKit.Framework”。
然后您应该能够使用如下代码添加事件:
You can do this using the Event Kit framework in OS 4.0.
Right click on the FrameWorks group in the Groups and Files Navigator on the left of the window. Select 'Add' then 'Existing FrameWorks' then 'EventKit.Framework'.
Then you should be able to add events with code like this:
是的,仍然没有 API (2.1)。 但似乎在 WWDC 上很多人已经对该功能感兴趣(包括我自己),建议访问以下站点并为此创建一个功能请求。 如果有足够的兴趣,他们最终可能会将 ICal.framework 转移到公共 SDK。
https://developer.apple.com/bugreporter/
Yes there still is no API for this (2.1). But it seemed like at WWDC a lot of people were already interested in the functionality (including myself) and the recommendation was to go to the below site and create a feature request for this. If there is enough of an interest, they might end up moving the ICal.framework to the public SDK.
https://developer.apple.com/bugreporter/
iPhone OS 4.0 中添加了日历访问:
Calendar access is being added in iPhone OS 4.0:
Swift 4.0 实现:
通过
import EventKit
在页面顶部使用导入,然后
Swift 4.0 implementation :
use import in top of page by
import EventKit
then
您可以使用 Tristan 概述的事件 API 添加事件,还可以添加显示在 iOS 日历中的 Google 日历事件。
使用 Google 的 API 目标 - C客户端
You can add the event using the Event API like Tristan outlined and you can also add a Google Calendar event which shows up in the iOS calendar.
using Google's API Objective-C Client
更新 swift 4 for Dashrath 答案
也不要忘记添加日历使用权限
Update for swift 4 for Dashrath answer
also don't forget to add permission for calendar usage
Swift-4.2 中的工作代码
现在我们将获得事件屏幕,您还可以在此处修改您的设置:
现在添加委托方法来处理取消并添加事件屏幕的事件按钮操作:
注意:不要忘记将 NSCalendarsUsageDescription 键添加到信息 plist 中。
Working code in Swift-4.2
Now we will get the event screen and here you can also modify your settings:
Now add delegate method to handle Cancel and add the event button action of event screen:
Note: Don't forget to add NSCalendarsUsageDescription key into info plist.
请记住将 endDate 设置为创建的事件,这是强制性的。
否则它将失败(几乎无声地)并出现以下错误:
对我来说完整的工作代码是:
Remember to set the endDate to the created event, it is mandatory.
Otherwise it will fail (almost silently) with this error:
The complete working code for me is:
谷歌的想法很好,但也存在问题。
我可以成功打开 Google 日历活动屏幕 - 但仅限于主桌面版本,并且在 iPhone Safari 上无法正常显示。 Google 移动日历在 Safari 上可以正常显示,但似乎无法使用 API 添加事件。
目前,我看不出有什么好的办法来解决这个问题。
The Google idea is a nice one, but has problems.
I can successfully open a Google calendar event screen - but only on the main desktop version, and it doesn't display properly on iPhone Safari. The Google mobile calendar, which does display properly on Safari, doesn't seem to work with the API to add events.
For the moment, I can't see a good way out of this one.
简单......使用tapku库......你可以谷歌搜索这个词并使用它......它是开源的......享受......不需要调试这些代码......
Simple.... use tapku library.... you can google that word and use it... its open source... enjoy..... no need of bugging with those codes....