【问题标题】:Add location to EKEvent IOS Calendar将位置添加到 EKEvent IOS 日历
【发布时间】:2014-11-13 07:53:20
【问题描述】:

如何不仅添加 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
}];

【问题讨论】:

  • 尝试做ans stackoverflow.com/questions/16647996/…中给出的操作
  • 找到解决方案了吗?我在同一条船上
  • @jsetting32 仍然没有解决方案,我认为这可能是不可能的:P
  • 好吧,我在想,因为您在苹果日历应用程序中创建事件时只需输入位置,因此位置字段只是一个字符串。仅此而已。
  • @jsetting32 是的,我认为日历应用程序使用的字符串可能有某种格式,我们需要在以编程方式创建事件时创建一个格式与纬度、经度和地名相同的字符串

标签: ios objective-c ekevent ekeventkit ekeventstore


【解决方案1】:

structuredLocation 属性在 iOS 9 上可用,虽然 EKEvent 文档没有提到它(更新:终于是 here),但 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

【讨论】:

    【解决方案2】:

    很奇怪,没有相关文档,但这是您将地理位置添加到日历事件的方式。

    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"];
    

    【讨论】:

    • 我不怀疑这行得通。但是文档提到使用 EKStructuredLocation 专门用于设置地理围栏位置。此解决方案可能会出现任何问题?
    • @spacyRicochet - 我对这种方法没有任何问题。从我玩过的情况来看,这是将地图添加到您的日历事件的唯一解决方案。我真的认为问题在于文档需要更新。要么他们实现了这个功能并且这是一个错误,要么他们只是忘记在他们的文档中包含这个功能。无论如何,它有效。
    • 对我来说似乎无法正常工作,尽管这可能是模拟器不喜欢它。稍后将在设备上试用。
    • @SpacyRicochet 它适用于 iPhone 5。我还没有在其他设备上测试过。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多