请求蓝牙权限:
//蓝牙
if #available(iOS 13.1, *) {
let autostate = CBManager.authorization
if(autostate == .notDetermined){
print("")
self.manager = CBCentralManager(delegate: nil, queue: DispatchQueue.main,options: [CBCentralManagerOptionShowPowerAlertKey:false])
// manager.state
}
} else {
let manager = CBCentralManager(delegate: nil, queue: DispatchQueue.main)
let state:CBManagerAuthorization = manager.authorization
switch(state){
case .notDetermined:
// KdBLEService.shared().requestBleToothPermission()
break
case .restricted:
break
case .denied:
break
case .allowedAlways:
break
@unknown default:
break
}
}
无线网权限:
CTCellularData().cellularDataRestrictionDidUpdateNotifier = {state in
switch state {
case .restrictedStateUnknown:
//未知
item.pstate = .unknown
let reachAbility:Reachability = Reachability.forInternetConnection()
let wifiState:NetworkStatus = reachAbility.currentReachabilityStatus()
switch(wifiState){
case NotReachable:
item.serviceState = false
break
default:
break
}
case .restricted:
//受限制
if(item.pstate != .stricted){
item.pstate = .stricted
self.stateAsyncUpdate?()
}
item.pstate = .stricted
case .notRestricted:
//Reachability库文中尾部放入
//不受限制
print("第一次进来也会显示这个状态,第一次进来需要")
let reachAbility:Reachability = Reachability.forInternetConnection()
let wifiState:NetworkStatus = reachAbility.currentReachabilityStatus()
switch(wifiState){
case NotReachable:
item.serviceState = false
break
default:
break
}
if(item.pstate != .on){
item.pstate = .on
}
self.stateAsyncUpdate?()
item.pstate = .on
@unknown default:
print("")
}
本地网络权限:
class LocalNetworkPermissionTester {
var connection: NWConnection
var success = false
var semaphore: DispatchSemaphore
init(semaphore: DispatchSemaphore) {
self.semaphore = semaphore
let dispatchQueue = DispatchQueue(label: "LocalNetworkPermissionTester")
self.connection = NWConnection(host: "127.255.255.255", port: 9, using: .udp)
self.connection.stateUpdateHandler = { state in
switch state {
case .ready:
self.success = true
semaphore.signal()
case .waiting(_):
// if #available(iOS 14.2, *) {
// print("reason: \(self.connection.currentPath?.unsatisfiedReason)")
// if case .localNetworkDenied? = self.connection.currentPath?.unsatisfiedReason {
// self.success = false
// semaphore.signal()
// }
// } else {
print("status: \(self.connection.currentPath?.status)")
self.success = false
semaphore.signal()
// }
default:
break
}
}
connection.start(queue: dispatchQueue) // this will trigger the prompt if necessary
}
}
public func getLocalNetPermission(item:KdPermissionItem){
// Task.init {
// do{
// let isOK = try await KdLocalNetworkManager().requestAuthorization()
// print("ok")
// if(!isOK){
// item.pstate = .stricted
// }else{
// item.pstate = .unknown
// }
// self.stateAsyncUpdate?()
// }catch{
// print("exception")
//
// item.pstate = .unknown
// self.stateAsyncUpdate?()
// }
// }
// item.pstate = .on
let semaphore = DispatchSemaphore(value: 0)
let tester = LocalNetworkPermissionTester(semaphore: semaphore)
semaphore.wait()
print("success: \(tester.success)")
if(tester.success){
if(item.pstate != .on){
item.pstate = .on
self.stateAsyncUpdate?()
}
item.pstate = .on
}else{
if(item.pstate != .stricted){
item.pstate = .stricted
self.stateAsyncUpdate?()
}
item.pstate = .stricted
}
item.isNeedGet = true
}