Add Location to EKEvent IOS Calendar

How to add location not only NSString, but also with latitude and longitude, so the map is also displayed on 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; 

The 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

+5
source share
3 answers

The structuredLocation property is available on iOS 9, although the EKEvent documentation does not mention this, but structuredLocation exists in the EKEvent public header file, and you can check it inside Xcode. There is no need to use KVC to install after iOS 9.

Quick version:

 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 
+7
source

It’s rather strange that there is no documentation for this, but that’s how you add geolocation to the 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"]; 
+5
source

Perhaps you can use setValue: ForKey: on EKEvent, after creating the EKStructuredLocation, the key is 'structuredLocation'

0
source

Source: https://habr.com/ru/post/1206780/


All Articles