【发布时间】:2019-10-18 16:56:42
【问题描述】:
我有在后台启用位置更新的应用程序代码
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters // less batery ussage
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.allowsBackgroundLocationUpdates = true
我想根据用户地理位置触发本地通知。
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { [weak self] (granted, error) in
guard let self = self else { return }
if granted {
print("NotificationCenter Authorization Granted!")
self.notificationCenter.removePendingNotificationRequests(withIdentifiers: [model.id])
let content = UNMutableNotificationContent()
content.title = model.name
content.body = model.desc
content.categoryIdentifier = "Location Notification"
content.userInfo["model_id"] = model.id
content.sound = UNNotificationSound.default
let region = self.makeRegion(for: model)
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let request = UNNotificationRequest(identifier: restaurant.id, content: content, trigger: trigger)
self.notificationCenter.add(request)
}
}
locationManager.startUpdatingLocation()
现在,当应用程序处于前台或后台时,我一直有关于位置更新的蓝色状态栏指示器。我认为一直出现对最终用户来说是很碍眼的。
我听说这将在用户授予 Core Location 的 While in Use 授权状态时显示。所以据我了解,我应该只在应用程序处于前台时启用触发此通知(但这里需要进行哪些更改?allowsBackgroundLocationUpdates = false ?)
并且仅在用户授予“始终授权”状态时才使用后台用户位置更新。
但奇怪的是,当应用程序处于前台时,也会出现这个蓝色的 bard 指示器。
更新
好的,我设置了“下次询问”,然后始终显示蓝色指示器。
如果有“使用中”,则蓝色指示器仅在背景中。
它在这里总是根本没有蓝色指示器。
我考虑添加这样的委托方法
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("Core Location authorization status: \(status)")
switch status {
case .authorizedAlways:
locationManager.allowsBackgroundLocationUpdates = true
default:
locationManager.allowsBackgroundLocationUpdates = false
}
}
我认为它应该解决蓝色指示器在状态栏中的突出显示。 唯一的问题是,如果 iOS 13 中的用户选择使用时,则不会尝试在后台更新位置,因此最后不会有机会显示有关始终授权的问题的警报,并且唯一是否有可能是用户手动打开“始终在设置”中?
所以我的问题是如何在 iOS 13 中以这种方式制作带有始终授权问题的警报,然后才会更改
.allowsBackgroundLocationUpdates = true
到
.allowsBackgroundLocationUpdates = false
【问题讨论】:
标签: ios core-location uilocalnotification