sparrowcode / PermissionsKit

Universal API for request permission and get its statuses.
https://x.com/sparrowcode_ios
MIT License
5.64k stars 462 forks source link

SPBluetoothPermission - Request Permission Suggestion #198

Closed leite-paulohf closed 4 years ago

leite-paulohf commented 4 years ago
#if os(iOS) && SPPERMISSION_BLUETOOTH

import UIKit
import CoreBluetooth

class SPBluetoothPermission: NSObject, SPPermissionProtocol {

    typealias SPBluetoothPermissionHandler = ()->()?
    private var completion: SPBluetoothPermissionHandler?
    private var manager: CBCentralManager?

    var isAuthorized: Bool {
        if #available(iOS 13.0, *) {
            return CBCentralManager().authorization == .allowedAlways
        }
        return CBPeripheralManager.authorizationStatus() == .authorized
    }

    var isDenied: Bool {
        if #available(iOS 13.0, *) {
            return CBCentralManager().authorization == .denied
        }
        return CBPeripheralManager.authorizationStatus() == .denied
    }

    func request(completion: @escaping ()->()?) {
        self.completion = completion
        self.manager = CBCentralManager(delegate: self, queue: nil, options: [:])
    }
}

extension SPBluetoothPermission: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if #available(iOS 13.0, *) {
            switch central.authorization {
            case .notDetermined: break
            default: self.completion?()
            }
        } else {
            switch CBPeripheralManager.authorizationStatus() {
            case .notDetermined: break
            default: self.completion?()
            }
        }
    }

}

#endif
ivanvorobei commented 4 years ago

Thanks! Tomorrow I will test it and add. I hope it solved problems)

ivanvorobei commented 4 years ago

Added!