我正在使用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