【问题标题】:How to trigger an EventKit alarm with Coordinates in Swift如何在 Swift 中使用坐标触发 EventKit 警报
【发布时间】:2015-05-08 22:40:55
【问题描述】:

我不知道其他人,但 EventKit 似乎很少有在线资源和教程供您参考以寻求帮助。

当用户点击一组坐标的半径时,我需要触发警报,我不确定最好的方法,我在本地通知和 EventKit 提醒之间徘徊。

我决定使用 eventKit,因为我觉得我可以用更多的警报做更多的事情,这是最实用的方法,但是我对 EventKit 了解不多,我遇到了一些问题。

无论如何,我已经设法聚在一起并构建了一个示例项目,该项目可以在用户离开当前位置时触发警报,唯一的问题是,我几乎想要做完全相反的事情,我想要为了在用户输入一组坐标时触发警报,我假设大部分代码都是可转移的,但我似乎主要停留在一个位上。

// Creates an EKStructuredLocation Instance with a title of "Current Location"
    let location = EKStructuredLocation(title: "Current Location")
    // Uses the last location update extracted from the locations array to supply you're current location
    location.geoLocation = locations.last as! CLLocation


    // Location is added to a newly created alarm instance
    let alarm = EKAlarm()
    alarm.structuredLocation = location
    // This alarm is triggered when the user moves away from the location proximity
    alarm.proximity = EKAlarmProximityLeave
    stationReminder.addAlarm(alarm)

我正在努力寻找如何将警报的位置设置为坐标而不是用户位置。

我尝试改变这个

location.geoLocation = locations.last as! CLLocation

location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")

但这不起作用,我相信我在正确的轨道上,但我抛出了这个错误:Cannot assign a value of type 'CLCircularRegion!' to a value of type 'CLLocation!'

我尝试了很多事情都没有解决,有没有人有这方面的经验并且知道如何提供帮助?

我还假设我必须从这里更改以下内容

alarm.proximity = EKAlarmProximityLeave

到这里

alarm.proximity = EKAlarmProximityEnter

更新

我已经在下面加入了一些 cmets 并尝试了很多其他的东西来让它工作,我觉得我很接近,但只是缺少一些东西。我无法触发此警报。请原谅所有代码 cmets,这只是为了让您可以看到我为解决此问题所做的一些尝试。

谁能看出这个警报代码有什么问题?

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    // Stops location manager from sending further updates
    locationManager.stopUpdatingLocation()

    // Creates a new EKReminder which is named and initialised with text from the UITextField
    let stationReminder = EKReminder(eventStore: appDelegate!.eventStore)
    stationReminder.title = locationText.text

    // Stores the previously created EKReminder in the default calendar
    stationReminder.calendar = appDelegate!.eventStore!.defaultCalendarForNewReminders()

    // Creates an EKStructuredLocation Instance with a title of "Current Location"
    let location = EKStructuredLocation(title: "Destination: Bournemouth Station")
    // Uses the last location update extracted from the locations array to supply you're current location
//        location.geoLocation = locations.last as! CLLocation
//        location.geoLocation = CLCircularRegion(circularRegionWithCenter: CLLocationCoordinate2D(latitude: 37.33233141, longitude: -122.03121860), radius: 50.0, identifier: "Location1")
//        location.geoLocation = CLLocation(latitude: 50.742771, longitude: -1.895072)
    location.radius = 50.0

    location.geoLocation = CLLocation(latitude:50.742771, longitude:-1.895072)
//        location.radius = 10.0 // metres

    // Location is added to a newly created alarm instance
    let alarm = EKAlarm()
    alarm.structuredLocation = location
    // This alarm is triggered when the user moves away from the location proximity
//        alarm.proximity = EKAlarmProximityEnter
    alarm.proximity = EKAlarmProximityEnter // "geofence": we alarm when *arriving*
    // but this will have no effect until Reminders is granted Location access...
    // and in iOS 8 it won't even ask for it until it is launched
    // also, in iOS 8 the separate background usage pref is withdrawn;
    // instead, auth of Reminders for "when in use" covers this...
    // ...because it means "this app *or one of its features* is visible on screen"
    stationReminder.addAlarm(alarm)

    // Now we have a fully configured reminder which we save in the Event Store
    var error: NSError?
    appDelegate!.eventStore?.saveReminder(stationReminder,
        commit: true, error: &error)

    if error != nil {
        println("Reminder failed with error \(error?.localizedDescription)")
    }
}

【问题讨论】:

    标签: ios swift core-location eventkit


    【解决方案1】:

    看起来 EKEvent 有一个 EKStructuredLocation,您使用正确。但是,您需要注意 geoLocation 属性的类型。它应该是一个 CLLocation,它与一个 CLCircularRegion 不同。

    修复步骤:

    1. 查看 EKStructuredLocation https://developer.apple.com/library/mac/documentation/EventKit/Reference/EKStructuredLocationClassRef/index.html 的文档

    2. 将 location.geoLocation 设置为您根据纬度、经度坐标创建的 CLLocation。 (查看CLLocation 的文档:https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocation_Class/index.html#//apple_ref/swift/cl/CLLocationgeoLocation = CLLocation(latitude: 37.33233141, longitude: -122.03121860)

    3. 单独设置geoLocation.radius location.radius = 50.0
    4. 此时设置 proximityEnter 应该可以按预期工作

    【讨论】:

    • 对不起,我应该在帖子中更清楚地说明,我正在努力与 CLLocationManager 合作是 EKStructuredLocation,我尝试了很多东西,但我就是不明白它
    • 尝试我的回答中的第 2 步和第 4 步。他们将修复此错误:“无法分配 'CLCircularRegion!' 类型的值!到“CLLocation!”类型的值。如果您需要更多帮助,请编辑您的问题。
    • 太棒了。这看起来应该在 appdelegate 的事件存储中设置一个 stationalarm。当用户到达纬度、经度地理位置的 50m 范围内时,应触发 Stationalarm。现在我有两个问题:1)你是如何测试它的? 2)什么“不起作用”?
    • 我已经通过输入 150 米外的坐标来测试坐标,然后我步行到正确的地方进行测试,您可以想象这是一项相当乏味的任务。似乎事件正在存储到提醒中,但是当你到达半径时没有警报或任何事情发生,这让我觉得与位置有关的事情没有设置正确
    • 听起来很糟糕。我要离线了,但现在有几个提示: - 添加一些日志记录,以便您可以在触发事件时在控制台中看到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多