Swift 通知、相機、相冊權限 封裝
//
// YAuthorization.swift
// ZXCloud
//
// Created by Seasons on 2020/2/27.
// Copyright © 2020 zhongxiao. All rights reserved.
//
import UIKit
import Photos
import AssetsLibrary
import AVFoundation
typealias AuthHandel = (Bool) -> Void
class YAuthorization: NSObject {
static let shared = YAuthorization()
//MARK: - 通知
func notificationAuth(handel:@escaping AuthHandel) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings { (set) in
DispatchQueue.main.async {
if set.authorizationStatus == UNAuthorizationStatus.authorized {
handel(true)
} else {
handel(false)
}
}
}
} else {
let open = UIApplication.shared.isRegisteredForRemoteNotifications
handel(open)
}
}
//MARK: - 判斷相機權限
func cameraAuth(handel:@escaping AuthHandel) {
let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
if authStatus == .notDetermined {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { (granted) in
handel(granted)
}
} else if authStatus == .restricted || authStatus == .denied {
handel(false)
} else {
handel(true)
}
}
//MARK: - 獲取相冊權限
func albumAuth(handel:@escaping AuthHandel) {
let authStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == .notDetermined {
PHPhotoLibrary.requestAuthorization { (states:PHAuthorizationStatus) in
if states == .authorized {
handel(true)
} else {
handel(false)
}
}
} else if authStatus == .restricted || authStatus == .denied {
handel(false)
} else {
handel(true)
}
}
}
調用eg:
// 相機
YAuthorization.shared.cameraAuth { (auth) in
if auth {
DispatchQueue.main.async {
}
}
}
info.plist文件
有問題或建議 請聯繫