ios - iOS蓝色状态栏同时使用地理围栏/背景位置更新来触发通知
问题描述
我有在后台启用位置更新的应用程序代码
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()
现在,当应用程序处于前台或后台时,我一直有关于位置更新的蓝色状态栏指示器。我认为一直出现对最终用户来说是很碍眼的。
我听说这将在用户授予核心位置的使用授权状态时显示。因此,据我了解,我应该只在应用程序处于前台时启用触发此通知(但这里需要进行哪些更改allowsBackgroundLocationUpdates = false
??)
并且仅在用户授予“始终授权”状态时才使用后台用户位置更新。
但奇怪的是,当应用程序处于前台时,这个蓝色的 bard 指示器也会出现。
更新
好的,我设置了“下次询问”,然后始终显示蓝色指示器。
如果有 While in Use,则蓝色指示器仅在背景中。
它在这里总是根本没有蓝色指示器。
我考虑添加这样的委托方法
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
解决方案
问题是你所做的一切都是错误的。
您没有进行后台位置监控,因此您不应该要求始终授权。你不需要它,最后你也得不到它。您应该请求WhenInUse 授权,仅此而已。(这对于通知地理围栏触发器来说已经足够了。)
至于蓝条,规则与往常一样:你应该设置
locationManager.allowsBackgroundLocationUpdates = true
仅当您即将进入后台而您已经在更新位置时,您应该设置
locationManager.allowsBackgroundLocationUpdates = false
当你回到前台时。如果您在更新位置时实际上并没有在后台运行(具有后台功能),请不要使用allowsBackgroundLocationUpdates
。
做你应该做的,一切都会好起来的。
推荐阅读
- c++ - 在图像扫描上下文中理解嵌套数组 c++
- sql-server - 为什么图像无法保存在数据库中
- python - 如何进行跨越两个表的 Django 查询?
- javascript - 有没有办法使用通配符属性过滤对象数组?
- python - 如何将表单中的文本保存到文件中?
- django - “详细信息”:“未找到具有给定凭据的活动帐户” simplejwt
- python-3.x - 如何使用精美的索引并使用 2D 索引数组的维度对 numpy 3D 数组进行切片
- java - JavaFX:在主类之外更改 CSS 文件
- vue.js - 如何测试在 jest 中安装方法内部的全局事件总线
- javascript - 如何在打字稿中将函数内部的承诺暴露给外部?