How to add geofencing Reminders to iOS programically

Reference : Calendar and Reminders Programming Guide from iOS Developer Library

Apple provides a way to add a Reminder through EventKit from iOS 6.

1. Before start coding, you should add the EventKit Framework to the target first.

eventkit

2. Check whether iOS is over 6. (Reminder API is available above iOS6)

 

1
2
3
4
// Check API (above iOS6)
if ([[EKEventStore class] instancesRespondToSelector:@selector(requestAccessToEntityType:completion:)] == NO) {
    return nil;
}

3. Init EKEventStore and request access.

5
6
7
8
9
10
11
12
13
EKEventStore *store = [[EKEventStore alloc] init];
 
[store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
    // handle access here
    if (granted) {
        // Create Reminder... [1]
    }
    [store release];
}];

4. If access is granted, Create reminder and add location alarm. (Code should be inside [1])

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
EKReminder *reminder = [EKReminder reminderWithEventStore:store];
reminder.title = @"Geofence Reminder";
reminder.calendar = [store defaultCalendarForNewReminders]; // Use default calendar for new reminders
 
EKAlarm *alarm = [[EKAlarm alloc] init];
 
EKStructuredLocation *location = [EKStructuredLocation locationWithTitle:@"Location title"];
CLLocation *clLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
location.geoLocation = clLocation;
[clLocation release];
 
[alarm setStructuredLocation:location];
alarm.proximity = EKAlarmProximityEnter; // Remind when enter or leave
[reminder addAlarm:alarm];
[alarm release];
 
// Save reminder
NSError *error;
[store saveReminder:reminder commit:YES error:&error];
if (error != nil) {
    NSLog(@"reminder error:%@",[error description]);
}

Done!