如何使用 Swift 中的坐标触发 EventKit 警报
How to trigger an EventKit alarm with Coordinates in Swift
我不知道其他人如何,但是 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
更新
我已经采纳了下面的一些评论并尝试了很多其他的方法来让它工作,我觉得我很接近但缺少一些东西。我无法触发此警报。请原谅所有的代码注释,这只是为了让您看到我为解决此问题所做的一些尝试。
有人能看出这个警报代码有什么问题吗?
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)")
}
}
看起来 EKEvent 有一个 EKStructuredLocation
,您使用正确。但是,您需要注意 geoLocation 属性 的类型。它应该是一个 CLLocation,它与 CLCircularRegion 不同。
修复步骤:
检查 EKStructuredLocation
https://developer.apple.com/library/mac/documentation/EventKit/Reference/EKStructuredLocationClassRef/index.html
的文档
将 location.geoLocation 设置为您根据纬度、经度坐标创建的 CLLocation。 (查看 CLLocation
的文档:https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocation_Class/index.html#//apple_ref/swift/cl/CLLocation)
geoLocation = CLLocation(latitude: 37.33233141, longitude: -122.03121860)
- 单独设置geoLocation.radius
location.radius = 50.0
- 此时设置
proximityEnter
应该会如您所愿
我不知道其他人如何,但是 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
更新
我已经采纳了下面的一些评论并尝试了很多其他的方法来让它工作,我觉得我很接近但缺少一些东西。我无法触发此警报。请原谅所有的代码注释,这只是为了让您看到我为解决此问题所做的一些尝试。
有人能看出这个警报代码有什么问题吗?
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)")
}
}
看起来 EKEvent 有一个 EKStructuredLocation
,您使用正确。但是,您需要注意 geoLocation 属性 的类型。它应该是一个 CLLocation,它与 CLCircularRegion 不同。
修复步骤:
检查
EKStructuredLocation
https://developer.apple.com/library/mac/documentation/EventKit/Reference/EKStructuredLocationClassRef/index.html 的文档
将 location.geoLocation 设置为您根据纬度、经度坐标创建的 CLLocation。 (查看
CLLocation
的文档:https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocation_Class/index.html#//apple_ref/swift/cl/CLLocation)geoLocation = CLLocation(latitude: 37.33233141, longitude: -122.03121860)
- 单独设置geoLocation.radius
location.radius = 50.0
- 此时设置
proximityEnter
应该会如您所愿