// 是否开启定位权限
class func IsOpenLocation() -> Bool{
let authStatus = CLLocationManager.authorizationStatus()
return authStatus != .restricted && authStatus != .denied
}
// 检测是否开启定位
if Tools.IsOpenLocation() == false {
// 如果没有开启定位,弹窗提示用户开启定位
createAlertView()
}
// 创建alert弹窗
func createAlertView() {
let alertView = UIAlertController(title: "是否开启定位?", message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let sureAction = UIAlertAction(title: "开启", style: .default) { _ in
self.openLocation()
}
alertView.addAction(cancelAction)
alertView.addAction(sureAction)
present(alertView, animated: true, completion: nil)
}
// 跳转到设置
func openLocation() {
let settingUrl = URL(string: "App-Prefs:root=LOCATION_SERVICES")
if let url = settingUrl, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
检测APP有没有开启定位权限,如果没有,跳转到设置
未经允许不得转载:IDEA激活码 » 检测APP有没有开启定位权限,如果没有,跳转到设置