?
Solved

Retrieving specific iOS Calendar events

Posted on 2013-01-18
2
Medium Priority
?
809 Views
Last Modified: 2013-01-23
I need to retrieve all iOS events, from all calendars, with a predefined location string of "Office", then write the result to the NSLog and a UITextView.

Here is my code thus far:

#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

Also, l gather that you can subscribe to the event store for notifications / changes to the calendar/s? How would this work in the context of the above? I am assuming that this would take the place of the button action, especially if the application was set to enter the background?
0
Comment
Question by:Greg_Arden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
binaryevo earned 1500 total points
ID: 38810122
If I remember correctly, and if I understand your question, this code should give you what you are looking for:

Return all calendars
NSArray *calList = [eventStore calendars];

Open in new window


Then you could slap that in a for loop and should be able to do a:

NSString *searchTitle = @"myTitle";
NSLog(@"Test Cal Event Thing: %@", [[calList objectAtIndex:index] searchTitle]);

Open in new window


Give that a shot.  Hope this helps!
0
 

Author Closing Comment

by:Greg_Arden
ID: 38810474
Thank you. I actually had a fresh look at this, and although what l implemented had a few additional features (NSDate formatting to NSString, converting the location string to all lowercase characters, in principal your suggestion is essentially what l implemented.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question