将位置添加到EKEvent IOS日历

时间:2014-11-13 07:53:20

标签: ios objective-c ekevent ekeventkit ekeventstore

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

<EKCalendarItem>  

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

@property(nonatomic, copy) NSString *location;

代码:

 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

3 个答案:

答案 0 :(得分:12)

属性structuredLocation在iOS 9上可用,虽然EKEvent文档没有提到它,但是在EKEvent公共头文件中确实存在structuredLocation,你可以在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

答案 1 :(得分: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"];

答案 2 :(得分:0)

您可以在创建EKStructuredLocation后使用setValue:ForKey:在EKEvent上,键是'structuredLocation'