Commit 25b309d9 authored by Steven杜宇's avatar Steven杜宇

// 权限设置

parent bc9c99c4
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// //
import UIKit import UIKit
import Photos
enum YHPermissionType: Int { enum YHPermissionType: Int {
case none = 0 case none = 0
...@@ -31,11 +32,9 @@ class YHPermissionItem { ...@@ -31,11 +32,9 @@ class YHPermissionItem {
class YHMyPermissionSettingVC: YHBaseViewController { class YHMyPermissionSettingVC: YHBaseViewController {
var items:[YHPermissionItem] = [YHPermissionItem(title: "网络".local, type:.network, enableStatus: true), let items:[YHPermissionItem] = [YHPermissionItem(title: "网络".local, type:.network, enableStatus: YHNetworkStatusManager.shared.isNetWorkOK),
YHPermissionItem(title: "照片".local, type:.photo, enableStatus: true), YHPermissionItem(title: "照片".local, type:.photo, enableStatus: false),
YHPermissionItem(title: "相机".local, type:.camera, enableStatus: false), YHPermissionItem(title: "相机".local, type:.camera, enableStatus: false)]
YHPermissionItem(title: "位置".local, type:.location, enableStatus: true),
YHPermissionItem(title: "文件".local, type:.file, enableStatus: false)]
lazy var tableView: UITableView = { lazy var tableView: UITableView = {
let tableView = UITableView(frame:.zero, style:.grouped) let tableView = UITableView(frame:.zero, style:.grouped)
...@@ -64,6 +63,72 @@ class YHMyPermissionSettingVC: YHBaseViewController { ...@@ -64,6 +63,72 @@ class YHMyPermissionSettingVC: YHBaseViewController {
make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar) make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar)
make.bottom.equalToSuperview().offset(-k_Height_TabBar) make.bottom.equalToSuperview().offset(-k_Height_TabBar)
} }
// 获取相册权限
requestPhotoLibraryPermission {
[weak self] grant in
guard let self = self else { return }
for item in items {
if item.type == .photo {
item.enableStatus = grant
self.tableView.reloadData()
}
}
}
// 获取相机权限
requestCameraPermission {
[weak self] grant in
guard let self = self else { return }
for item in items {
if item.type == .camera {
item.enableStatus = grant
self.tableView.reloadData()
}
}
}
}
func requestPhotoLibraryPermission(completion: @escaping (Bool) -> Void) {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
// 已经授权
completion(true)
case .denied, .restricted, .limited:
// 拒绝或受限制,需要提示用户去设置中授权
completion(false)
case .notDetermined:
// 尚未确定,请求授权
completion(false)
@unknown default:
completion(false)
}
}
func requestCameraPermission(completion: @escaping (Bool) -> Void) {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized:
// 已经授权
completion(true)
case .denied, .restricted:
// 拒绝或受限制,需要提示用户去设置中授权
completion(false)
case .notDetermined:
// 尚未确定,请求授权
completion(false)
@unknown default:
completion(false)
}
} }
} }
...@@ -88,27 +153,12 @@ extension YHMyPermissionSettingVC: UITableViewDelegate, UITableViewDataSource { ...@@ -88,27 +153,12 @@ extension YHMyPermissionSettingVC: UITableViewDelegate, UITableViewDataSource {
} }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if 0 <= indexPath.row && indexPath.row < items.count {
let item = items[indexPath.row]
if item.type == .photo || item.type == .camera { let url = URL(string: UIApplication.openSettingsURLString)
let url = URL(string: UIApplication.openSettingsURLString) if let url = url, UIApplication .shared.canOpenURL(url) {
if let url = url, UIApplication .shared.canOpenURL(url) { UIApplication .shared.open(url, options: [:], completionHandler: {
UIApplication .shared.open(url, options: [:], completionHandler: { (success) in
(success) in })
})
}
return
}
if item.type == .location {
if let url = URL(string: "App-prefs:Photos") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
} else if item.type == .file {
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment