Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add location to EKEvent IOS Calendar

How to add the location not just NSString but with latitude and longitude ,so it shows a map too in the Calendar?

<EKCalendarItem>  

https://developer.apple.com/LIBRARY/ios/documentation/EventKit/Reference/EKCalendarItemClassRef/index.html#//apple_ref/occ/instp/EKCalendarItem/location

@property(nonatomic, copy) NSString *location;

Code :

 EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Event Title";
    event.startDate = [NSDate date]; //today
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    event.notes=@"Note";
    event.location=@"Eiffel Tower,Paris"; //how do i add Lat & long / CLLocation?
    event.URL=[NSURL  URLWithString:shareUrl];
    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
}];

Example

like image 767
Metalhead1247 Avatar asked Nov 13 '14 07:11

Metalhead1247


People also ask

How do I add locations to my iPhone Calendar?

Open the Calendar app or Calendar widget. Tap the plus sign at the upper-right corner of your display. Add an event to your Calendar as usual by giving it a name, date, and time, but also be sure to add a location. Type the address into the Location field provided.

How do I turn on precise location on iPhone Calendar?

First, if you're working on an iOS device, make sure Calendar can access your location by going to Settings > Privacy > Location Services > Calendar and selecting While Using the App.


2 Answers

The property structuredLocation is available on iOS 9, though EKEvent documentation doesn't mention it (Update: it's finally here) , but structuredLocation does exist in EKEvent public header file, and you can check it inside Xcode. No need to use KVC to set it after iOS 9.

Swift version as follows:

let location = CLLocation(latitude: 25.0340, longitude: 121.5645)
let structuredLocation = EKStructuredLocation(title: placeName)  // same title with ekEvent.location
structuredLocation.geoLocation = location
ekEvent.structuredLocation = structuredLocation
like image 178
denkeni Avatar answered Sep 30 '22 17:09

denkeni


It's pretty bizarre that there is no documentation for this, but this is how you add a geoLocation to a calendar event.

EKStructuredLocation* structuredLocation = [EKStructuredLocation locationWithTitle:@"Location"]; // locationWithTitle has the same behavior as event.location
CLLocation* location = [[CLLocation alloc] initWithLatitude:0.0 longitude:0.0];
structuredLocation.geoLocation = location;

[event setValue:structuredLocation forKey:@"structuredLocation"];
like image 23
Michael E Avatar answered Sep 30 '22 17:09

Michael E