开发App过程中我们会遇到关于相机,位置等权限设置的问题,下面来讲一下关于这些权限的设置以及如何获取权限状态
一. 相机,相册
1.首先在info.plist文件中添加两个key NSCameraUsageDescription与NSPhotoLibraryUsageDescription,如下图所示:
2.调起相机相册使用权限
//打开相机权限
func openCamera() {
let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
if authStatus == .notDetermined {
AVCaptureDevice.requestAccess(for: .video) { (res) in
//此处可以判断权限状态来做出相应的操作,如改变按钮状态
if res{
DispatchQueue.main.async {
self.cameraBtn .setImage(UIImage(named: "turn on"), for: UIControl.State.normal)
}
}else{
DispatchQueue.main.async {
self.cameraBtn .setImage(UIImage(named: "turn off"), for: UIControl.State.normal)
}
}
}
}
}
//开启相册权限
func openAlbum() {
let authStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == .notDetermined {
PHPhotoLibrary.requestAuthorization({ (status) in
//此处可以判断权限状态来做出相应的操作,如改变按钮状态
if status == .authorized {
DispatchQueue.main.async {
self.albumBtn .setImage(UIImage(named: "turn on"), for: UIControl.State.normal)
}
} else if status == .denied || status == .restricted{
DispatchQueue.main.async {
self.albumBtn .setImage(UIImage(named: "turn off"), for: UIControl.State.normal)
}
}
})
}
}
3.在需要判断目前相机相册是否是开启状态时,代码如下:
//是否开启相机权限
func IsOpenCamera() -> Bool{
let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
return authStatus != .restricted && authStatus != .denied
}
//是否开启相册权限
func IsOpenAlbum() -> Bool{
let authStatus = PHPhotoLibrary.authorizationStatus()
return authStatus != .restricted && authStatus != .denied
}
二.麦克风
1.开启麦克风需在info.plist加入key NSMicrophoneUsageDescription
2.调起麦克风使用权限
//开启麦克风权限
func openAudioSession() {
let permissionStatus = AVAudioSession.sharedInstance().recordPermission
if permissionStatus == AVAudioSession.RecordPermission.undetermined {
AVAudioSession.sharedInstance().requestRecordPermission { (granted) in
//此处可以判断权限状态来做出相应的操作,如改变按钮状态
if granted{
DispatchQueue.main.async {
self.microphoneBtn .setImage(UIImage(named: "turn on"), for: UIControl.State.normal)
}
}else{
DispatchQueue.main.async {
self.microphoneBtn .setImage(UIImage(named: "turn off"), for: UIControl.State.normal)
}
}
}
}
}
3.在需要判断目前麦克风是否是开启状态时,代码如下:
//是否开启麦克风
func IsOpenAudioSession() -> Bool{
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
return authStatus != .restricted && authStatus != .denied
}
三.位置
1.位置权限相对比较特殊,需要在info.plist文件中加入三个key,分别是NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription和
NSLocationUsageDescription 如下图:
2.调用位置权限
//位置请求
let locationManager:CLLocationManager?
locationManager = CLLocationManager.init()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
如上代码可以调用起系统的位置权限弹窗,位置权限状态改变时系统会调用它的代理方法:
//位置权限状态发生变化调用此方法
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted, .denied:
locationBtn .setImage(UIImage(named: "turn off"), for: UIControl.State.normal)
break
case .authorizedWhenInUse:
locationBtn .setImage(UIImage(named: "turn on"), for: UIControl.State.normal)
break
case .authorizedAlways:
locationBtn .setImage(UIImage(named: "turn on"), for: UIControl.State.normal)
break
case .notDetermined:
locationBtn .setImage(UIImage(named: "turn off"), for: UIControl.State.normal)
break
@unknown default:
break
}
}
3.在需要判断目前位置权限是否是开启状态时,代码如下:
//是否开启定位权限
func IsOpenLocation() -> Bool{
let authStatus = CLLocationManager.authorizationStatus()
return authStatus != .restricted && authStatus != .denied
}
以上就是对使用系统相机,相册,麦克风,位置权限使用的总结,如有问题,请指教!