我需要使用预定义的“Office”位置字符串从所有日历中检索所有 iOS 事件,然后将结果写入 NSLog 和 UITextView。
到目前为止,这是我的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize eventStore = _eventStore;
@synthesize events = _events;
@synthesize eventTextView;
- (void)viewDidLoad
{
// Store a reference to the event store
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,
NSError *error) {
// handle access here
if (granted)
NSLog (@"Access granted");
}];
self.eventStore = eventStore;
// Define a range of event dates we want to display
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:(-1*60*60)]; // 1 hour in
the past
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:(60*60*24*365)]; // 1 year
from now
// Create a predicate to search all celndars with our date range
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
endDate:endDate calendars:nil];
// Query the event store using the predicate.
NSArray *results = [self.eventStore eventsMatchingPredicate:predicate];
//Convert the results to a mutable array and store so we can implement swipe to
delete
NSMutableArray *events = [[NSMutableArray alloc] initWithArray:results];
self.events = events;
// Load the events from the event store
//[self reloadEvents:nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) reloadEvents:(id)sender
{
}
- (IBAction)getEvents:(id)sender {
int i;
int eventCount;
eventCount = [self.events count];
for (i = 0; i < eventCount; i++){
self.eventTextView.text = [[self.events objectAtIndex: i]title], [[self.events
objectAtIndex: i]startDate], [[self.events objectAtIndex: i]endDate], [[self.events
objectAtIndex: i]location];
NSLog (@"Event Title: %@, Event Start Date: %@, Event End Date: %@, Event
Location: %@",[[self.events objectAtIndex: i]title],
[[self.events objectAtIndex: i]startDate],
[[self.events objectAtIndex: i]endDate],
[[self.events objectAtIndex: i]location]);
}
}
@end
UITextView“eventTextView”中的结果是,我只得到了我的最后一个事件标题,没有别的,但在我的控制台中,我得到了我所要求的一切。我会以正确的方式解决这个问题吗?
另外,我收集到您可以订阅事件存储以获取日历的通知/更改?在上述情况下,这将如何工作?我假设这将取代按钮操作,特别是如果应用程序设置为进入后台?