すべてのカレンダーからすべての iOS イベントを取得し、事前定義された場所の文字列「Office」を使用して、結果を 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" の結果は、最後のイベント タイトルを取得するだけで、それ以外は何も取得しませんが、コンソールでは要求したすべてを取得します。私はこれを正しい方法で行っていますか?
また、通知/カレンダーの変更のためにイベントストアを購読できることを収集しますか? 上記のコンテキストでこれはどのように機能しますか?特にアプリケーションがバックグラウンドに入るように設定されている場合、これがボタンアクションの代わりになると思いますか?