/// 提示用户打开app需要的权限,并在用户同意后,跳转到对应的setting页面
/// - Parameter appAuthorityKind:请求何种权限
extension UIViewController {
func goToSetting(appAuthorityKind: AppAuthority) {
DispatchQueue.main.async(execute: { () -> Void in
let alertController = UIAlertController(title: appAuthorityKind.getAuthorityStr(),
message: "请求打开" + appAuthorityKind.getAuthorityStr(),
preferredStyle: .alert)
let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler:nil)
let settingsAction = UIAlertAction(title:"设置", style: .default, handler: {
(action) -> Void in
let url = URL(string: UIApplication.openSettingsURLString)
if let url = url, UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
})
} else {
UIApplication.shared.openURL(url)
}
}
})
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
self.present(alertController, animated: true, completion: nil)
})
}
}
enum AppAuthority: String {
case PhotoLibrary
case Camera
case Map
case BlueTooth
func getAuthorityStr() -> String {
switch self{
case .PhotoLibrary:
return "相册"
case.Camera:
return "相机"
case.Map:
return "地图"
case .BlueTooth:
return "蓝牙"
}
}
}