Ios 应用程序仅在使用时请求位置访问
我想使用requestAlwaysAuthorization,但出于某种原因,它只提供允许一次、使用时允许或不允许的选项。这是我的密码Ios 应用程序仅在使用时请求位置访问,ios,swift,swiftui,Ios,Swift,Swiftui,我想使用requestAlwaysAuthorization,但出于某种原因,它只提供允许一次、使用时允许或不允许的选项。这是我的密码 if CLLocationManager.locationServicesEnabled() { switch locationManager.authorizationStatus { case .notDetermined, .restricted, .denied: locat
if CLLocationManager.locationServicesEnabled() {
switch locationManager.authorizationStatus {
case .notDetermined, .restricted, .denied:
locationManager.requestAlwaysAuthorization()
case .authorizedAlways:
break
case .authorizedWhenInUse:
locationManager.requestAlwaysAuthorization()
@unknown default:
break
}
} else {
locationManager.requestAlwaysAuthorization()
从苹果公司的文档中: 您必须调用此方法或requestWhenInUseAuthorization()方法,应用程序才能接收位置信息若要调用此方法,您必须在应用程序的Info.plist文件中同时具有NSLocationAlwaysUsageDescription和NSLocationWhenUsageDescription密钥。当前授权状态为以下任一状态时,可以调用requestAlwaysAuthorization(): 未确定-CLAuthorizationStatus.notDetermined 使用时-CLAuthorizationStatus.authorizedWhenInUse 当用户做出权限选择时,使用CLLocationManager委托上的locationManager(uU2;:didUpdateLocations:)方法接收更新 核心位置限制对requestAlwaysAuthorization()的调用在应用程序调用此方法后,进一步调用不会产生任何影响 基于此,您的代码很少会出错
requestAlwaysAuthorization
的调用太多。这就足够了:Info.plist
中缺少NSLocationAlwaysUsageDescription
和nsLocationWhenUsageDescription
。在Info.plist中为这些定义非空字符串
我知道所有这些,但恐怕问题已经到了尽头。许多其他人尝试请求“始终授权”,但实际上,首先它请求使用中,然后在应用生命周期的后期,它请求更改为“始终授权”。您可以共享应用程序代码/应用程序吗?这是预期的行为。当您请求“始终”权限时,系统将提示用户“使用时”,您的应用程序将收到“临时始终”权限。在你的应用程序在后台实际使用位置和用户实际使用你的应用程序一段时间后,系统会提示他们升级到“始终”。在iOS 14.3及更高版本中,您可以在使用时询问,然后在获得“使用时”权限后询问“始终”
if CLLocationManager.locationServicesEnabled() {
switch locationManager.authorizationStatus {
case .notDetermined, .authorizedWhenInUse:
locationManager.requestAlwaysAuthorization()
default:
print("Cannot ask user for requestAlwaysAuthorization")
}
}