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{
}
Run Code Online (Sandbox Code Playgroud)
二手INTULocationManager,Swift 4.1,iOS 11.1
如果是第一次运行此代码,则会弹出“位置许可请求”
但是如果我拒绝,则下次不会弹出。
如何打开权限弹出?
我创建按钮
运行此代码
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
Run Code Online (Sandbox Code Playgroud)
但没有用
用户拒绝权限后,没有任何默认功能会弹出位置权限。您需要向用户显示警告,提示需要权限,然后将用户重定向到“设置”屏幕。这是您可以使用的完整代码。定义一个将检查位置权限的功能。
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
}
Run Code Online (Sandbox Code Playgroud)
现在,通过此功能检查位置权限,并在需要时显示警报。
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", style: UIAlertActionStyle.cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3569 次 |
| 最近记录: |