iOS Swift如何打开位置权限弹出窗口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS Swift如何打开位置权限弹出窗口相关的知识,希望对你有一定的参考价值。
var locMgr = INTULocationManager.sharedInstance()
locMgr.requestLocation(withDesiredAccuracy: .city, timeout: 30, delayUntilAuthorized: true,block: {(currentLoc: CLLocation!, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus) -> Void in
if status == INTULocationStatus.success {
}
else{
}
使用INTULocationManager,Swift 4.1,ios 11.1
如果第一次运行此代码弹出位置权限请求
但如果我否认,下次不会弹出。
如何打开Permission Pop up?
我创建了Button
运行此代码
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
但没有奏效
答案
用户拒绝权限后,没有任何默认功能会弹出位置权限。您需要向用户显示需要权限的警报,然后将用户重定向到“设置”屏幕。这是您可以使用的完整代码。定义一个将检查位置权限的函数。
func hasLocationPermission() -> Bool {
var hasPermission = false
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
hasPermission = false
case .authorizedAlways, .authorizedWhenInUse:
hasPermission = true
}
} else {
hasPermission = false
}
return hasPermission
}
现在通过此功能检查位置权限,并在需要时显示警报。
if !hasLocationPermission() {
let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
//Redirect to Settings app
UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel".localized(), style: UIAlertActionStyle.cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
另一答案
如果用户拒绝权限,则打开Permission PopUp
/* func checkLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse
{
print("requesting autorization")
locationManager.requestWhenInUseAuthorization()
} else {
print("start updating location")
}
}*/
func askEnableLocationService() ->String {
var showAlertSetting = false
var showInitLocation = false
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .denied:
showAlertSetting = true
print("HH: kCLAuthorizationStatusDenied")
case .restricted:
showAlertSetting = true
print("HH: kCLAuthorizationStatusRestricted")
case .authorizedAlways:
showInitLocation = true
print("HH: kCLAuthorizationStatusAuthorizedAlways")
case .authorizedWhenInUse:
showInitLocation = true
print("HH: kCLAuthorizationStatusAuthorizedWhenInUse")
case .notDetermined:
showInitLocation = true
print("HH: kCLAuthorizationStatusNotDetermined")
default:
break
}
}else{
showAlertSetting = true
print("HH: locationServicesDisabled")
}
if showAlertSetting {
let alertController = UIAlertController(title: "xxxxxx", message: "Please enable location service in the settings", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
if let url = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
alertController.addAction(OKAction)
self.window?.rootViewController?.present(alertController, animated: true, completion:nil)
}
if showInitLocation {
return "YES"
}
return "NO"
}
另一答案
这是默认行为。弹出窗口第一次显示。后续请求将被视为已拒绝或在首次选择时选择的任何内容。但是,您可以实施自己的警报并将用户直接发送到设置应用以授予位置访问权限,如下所示:
//check if user has denied the access on first popup
if !permissionGranted {
let permissionAlert = UIAlertController(title: "Location Access", message: "Requires location access to take advantage of this feature. Please provide location access from settings", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
let settingAction = UIAlertAction(title: "Settings", style: .default) { (action) in
guard let appSettingURl = URL(string: UIApplicationOpenSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(appSettingURl) {
UIApplication.shared.open(appSettingURl, options: [:], completionHandler: nil)
}
}
permissionAlert.addAction(cancelAction)
permissionAlert.addAction(settingAction)
present(permissionAlert, animated: true, completion: nil)
}
以上是关于iOS Swift如何打开位置权限弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MIUI 中打开 Draw Overlay 权限弹出窗口?