访问权限

import UIKit
import AVFoundation
import Photos
import CoreLocation
import Contacts
import CoreBluetooth
import EventKit


/*
 默认都是 .notDetermined 用户没有做出选择
*/

class PermissionManager: NSObject {

    /// 麦克风权限
    /// Privacy - Microphone Usage Description
    public static func isOpenMicrophone() -> Bool {
        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.audio)
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 访问相册权限
    /// Privacy - Photo Library Usage Description (读取)
    /// Privacy - Photo Library Additions Usage Description (写入)
    public static func isOpenPhotoLibrary() -> Bool {
        let authStatus = PHAuthorizationStatus.authorized
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 访问相机权限
    /// Privacy - Camera Usage Description
    public static func isOpenCamera() -> Bool {
        let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 推送权限
    public static func isOpenPush() -> Bool {
        let setting = UIApplication.shared.currentUserNotificationSettings
        if setting?.types == .alert ||
            setting?.types == .sound ||
            setting?.types == .badge {
                return true
        }
        return false
    }
    
    /// 定位权限
    /// Privacy - Location When In Use Usage Description (使用期间)
    /// Privacy - Location Always and When In Use Usage Description (总是)
    public static func isOpenLocation() -> Bool {
        let location = CLLocationManager.locationServicesEnabled()
        if !location {
            return false
        }
        
        let authStatus = CLLocationManager.authorizationStatus()
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 通讯录权限
    /// Privacy - Contacts Usage Description
    public static func isOpenContactStore() -> Bool {
        let authStatus = CNContactStore.authorizationStatus(for: .contacts)
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 蓝牙权限
    /// Privacy - Bluetooth Peripheral Usage Description
    public static func isOpenBluetooth() -> Bool {
        let authStatus = CBPeripheralManager.authorizationStatus()
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 日历权限
    /// Privacy - AppleEvents Sending Usage Description
    public static func isOpenEvent() -> Bool {
        let authStatus = EKEventStore.authorizationStatus(for: .event)
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
    
    /// 备忘录权限
    /// Privacy - Reminders Usage Description
    public static func isOpenReminder() -> Bool {
        let authStatus = EKEventStore.authorizationStatus(for: .reminder)
        if authStatus == .denied ||
            authStatus == .restricted {
            return false
        }
        return true
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容