【发布时间】: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