将位置添加到EKEvent IOS日历

Met*_*247 8 objective-c ios ekevent ekeventkit ekeventstore

如何添加位置不仅仅是NSString而且还有纬度和经度,所以它在日历中也显示了一个地图?

<EKCalendarItem>  
Run Code Online (Sandbox Code Playgroud)

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

@property(nonatomic, copy) NSString *location;
Run Code Online (Sandbox Code Playgroud)

代码:

 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
}];
Run Code Online (Sandbox Code Playgroud)

例

den*_*eni 13

该属性structuredLocation在iOS 9上可用,但EKEvent文档没有提及它,但structuredLocation确实存在于EKEvent公共头文件中,您可以在Xcode中检查它.在iOS 9之后无需使用KVC进行设置.

Swift版本如下:

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
Run Code Online (Sandbox Code Playgroud)


Mik*_*e E 6

很遗憾没有相关的文档,但这就是你如何将geoLocation添加到日历事件中.

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"];
Run Code Online (Sandbox Code Playgroud)