ios - 检查 EKCalendar
<p><p>美好的一天!
我通过 UIActivityItems 使用“将事件保存到日历”功能。在该函数中,我创建新日历并将事件添加到此日历:</p>
<pre><code>EKEventStore* eventStore = [ init];
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
if (!localSource)
return;
EKCalendar *newCalendar = ;
calendar.source = localSource;
calendar.title = @"New Calendar";
NSError *errorCalendar;
;
EKEvent *event= ;
event.title = @"Title";
event.startDate = startDate;
event.endDate = endDate;
;
// and etc.
</code></pre>
<p>及其工作。但每次下一次它都会再次创建名为“新日历”的新日历。如何检查具有该名称的日历是否已存在?以及如何更改日历类型?生日等。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,您需要在应用的生命周期中使用 <code>EventStore</code> 的单个实例,<a href="https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html#//apple_ref/doc/uid/TP40004775-SW1" rel="noreferrer noopener nofollow">according to Apple</a> . </p>
<p>因此我建议将 <code>eventStore</code> 设为 ViewController 的属性:</p>
<p><code>@property(非原子,保留)EKEventStore *eventStore;</code></p>
<p>在你的 <code>viewDidLoad:</code> </p>
<p><code>self.eventStore = [ init];</code></p>
<p>现在您可以在执行任何操作之前检查您正在读取和写入的同一 <code>eventStore</code> 实例:</p>
<pre><code>-(BOOL)checkForCalendar {
//get an array of the user's calendar using your instance of the eventStore
NSArray *calendarArray = ;
// The name of the calendar to check for. You can also save the calendarIdentifier and check for that if you want
NSString *calNameToCheckFor = @"New Calendar";
EKCalendar *cal;
for (int x = 0; x < ; x++) {
cal = ;
NSString *calTitle = ;
// if the calendar is found, return YES
if (() {
return YES;
}
}
// Calendar name was not found, return NO;
return NO;
}
-(void)saveNewEvent {
// If the calendar does not already exist, create it before you save the event.
if ( == NO) {
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
if (!localSource)
return;
EKCalendar *newCalendar = ;
calendar.source = localSource;
calendar.title = @"New Calendar";
NSError *errorCalendar;
;
}
EKEvent *event= ;
event.title = @"Title";
event.startDate = startDate;
event.endDate = endDate;
;
// and etc.
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 检查 EKCalendar,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16675838/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16675838/
</a>
</p>
页:
[1]