我正在使用UILocalNotification
中支持CLRegion
支持IOS 8功能的简单待办应用程序。本地通知在打到区域时不显示
这里是我使用安排本地通知代码:
var schedule = false
let notification = UILocalNotification()
/// Schedlue with date
if let reminder = self.dateReminderInfo {
schedule = true
notification.fireDate = reminder.fireDate
notification.repeatInterval = reminder.repeatInterval
notification.alertBody = self.title
}
/// Schedule with location
if let reminder = self.locationReminderInfo {
schedule = true
let region = CLCircularRegion(circularRegionWithCenter: reminder.place.coordinate, radius: CLLocationDistance(reminder.distance), identifier: taskObjectID)
region.notifyOnEntry = reminder.onArrive
region.notifyOnExit = reminder.onArrive
notification.region = region
notification.regionTriggersOnce = false
}
/// Schedule
if schedule {
notification.userInfo = ["objectID": taskObjectID]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
调度日期的作品。我安排通知,退出应用程序,并在适当的时间通知显示在屏幕上,很好。问题是当我安排通知与位置。我以米为单位传递坐标和半径(例如100米)。该应用程序不显示任何内容。我正在测试它在离家1公里的距离并到达那里。没有通知显示。我正在玩模拟器,并将位置从可用位置改变为靠近我的位置和后面的自定义位置。没有通知显示。哪里有问题?
在AppDelegate中,我注册了通知:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
这里是位置服务的代码。
func startLocationService() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
let status = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Denied {
let title = (status == CLAuthorizationStatus.Denied) ? "Location services are off" : "Background location is not enabled"
let message = "To use background location you must turn on 'Always' in the Location Services Settings"
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
/// Settings
alertController.addAction(UIAlertAction.normalAction("Settings", handler: { _ in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
return
}))
/// Cancel
alertController.addAction(UIAlertAction.cancelAction(String.cancelString(), handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
} else if status == CLAuthorizationStatus.NotDetermined {
/// nothing
}
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
CLLocationManager正在工作,因为我有很频繁调用的委托方法。
/// Mark: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
self.navigationItem.title = "\(location.coordinate.latitude)" + " " + "\(location.coordinate.longitude)"
println(self.navigationItem.title)
}
}
我想第二天解决问题,并找不到好的解决方案当用户出现在地方或离开地方时如何显示本地通知。我不知道如果使用UILocalNotification
region
属性对此解决方案有好处,或者我应该创建一种机制,它将搜索我已保存在应用程序中的位置并检查每次位置更改。对这个主题的任何意见也赞赏;)
谢谢你提前。
你解决了吗?我有同样的问题。谢谢! – Leandro 2015-03-07 01:36:36
不幸的不是。我创建了自己的机制,它正在与CLLocationManager一起工作,并检查是否访问过地点,然后触发适当的通知。像魅力一样工作 – 2015-03-07 13:35:36
对你有好处!我希望苹果解决这个问题。 – Leandro 2015-03-08 23:16:51