我在应用程序代码中具有启用后台位置更新的功能
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
?)
并且仅在用户授予“始终授权”状态后才使用后台用户位置更新。
但是奇怪的是,当应用程序处于前台时,这个蓝色的指示符也会出现。
UPDATE
确定我已经设置了下一次询问,然后始终显示蓝色指示符。
如果使用中,则蓝色指示器仅在背景中。
这里总是不存在蓝色指示器。
我考虑添加这种委托方法
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
。
只需执行您应做的事情,一切都会好起来的。