objective-c - 使用开始日期获取 iPhone 日历中的所有事件

标签 objective-c xcode nspredicate fetch ekeventstore

我有一个应用程序可以通过按钮提醒患者他/她的预约。 如果患者单击按钮,我想获取患者日历中的所有事件以检查约会是否已经存在。如果不存在,我将创建事件。

我知道如何将事件添加到日历中,但获取的结果是返回零计数??

下面是我的代码:

 //set start and end date
NSDate* startDate = [NSDate date];//start from today
NSDate* endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];//no end
NSLog(@"S Date %@", startDate);
NSLog(@"E Date %@", endDate);

NSString *AppointmentTitle=@"Appointment in Hospital at Pediatric Clinic";

EKEventStore *store = [[EKEventStore alloc] init];
NSArray *calendarArray = [store calendars];
NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
NSArray *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
BOOL IsFound;
int EventsCount=eventList.count;
for(int i=0; i < EventsCount; i++)
{
    NSLog(@"Event Title:%@", [[eventList objectAtIndex:i] title]);
    if ([[[eventList objectAtIndex:i] title] isEqualToString:AppointmentTitle])//check title
    {
        IsFound=TRUE;
        break;
    }
    else
    {
        IsFound=FALSE;
    }
}

此代码在 2 个月前运行良好。突然,当我回来测试它时,它没有用。我错过了什么?或者我的代码有什么错误吗?

我需要你的帮助......

最佳答案

最终我得到了答案, 我认为 Apple 做了一些允许的事情 ( 在 iOS 5 中,您只能访问事件存储中的事件 (EKEntityTypeEvent),而在 iOS 6 中您可以访问提醒 (EKEntityTypeReminder)。但是您需要以下代码才能至少获得 1 次授权。 ) “由 WrightsCS 回答”

找到下面的代码:-

   EKEventStore *store = [[EKEventStore alloc] init];


// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];


if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    /* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if ( granted )
         {
             NSLog(@"User has granted permission!");
             // Create the start date components
             NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
             oneDayAgoComponents.day = -1;
             NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                           toDate:[NSDate date]
                                                          options:0];

             // Create the end date components
             NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
             oneYearFromNowComponents.year = 1;
             NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                                toDate:[NSDate date]
                                                               options:0];

             // Create the predicate from the event store's instance method
             NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                                     endDate:oneYearFromNow
                                                                   calendars:nil];

             // Fetch all events that match the predicate
             NSArray *events = [store eventsMatchingPredicate:predicate];
             NSLog(@"The content of array is%@",events);
         }
         else
         {
             NSLog(@"User has not granted permission!");
         }
     }];
}

关于objective-c - 使用开始日期获取 iPhone 日历中的所有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909776/

相关文章:

ios - MMDrawerController 入门

ios - 错误 :Unknown type name BOOL for c function in Xcode

swift - 文件 'MainViewController.swift' 是模块 'Charts' 的一部分;忽略导入

ios - 核心数据一对一关系返回 NULL

core-data - CoreData 检查对多关系是否包含对象

ios - dSYM 地址查找

objective-c - 可以忽略这个调试错误 "[CoreBluetooth] XPC connection invalid"吗

ios - 是否应该为每个场景/屏幕创建单独的 ViewController.Swift?

ios - 从 'String' 转换为不相关类型 'LocationModel' 始终失败警告

iphone - 带有自定义单元格的 UITableView 缓慢滚动