访问权限

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
    }
}

你可能感兴趣的:(访问权限)