当前位置: 首页 > news >正文

swift获取app网络和本地网络权限

请求蓝牙权限:

 //蓝牙if #available(iOS 13.1, *) {let autostate = CBManager.authorizationif(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.authorizationswitch(state){case .notDetermined:
//                    KdBLEService.shared().requestBleToothPermission()breakcase .restricted:breakcase .denied:breakcase .allowedAlways:break@unknown default:break}}

无线网权限:

CTCellularData().cellularDataRestrictionDidUpdateNotifier = {state inswitch state {case .restrictedStateUnknown://未知item.pstate = .unknownlet reachAbility:Reachability = Reachability.forInternetConnection()let wifiState:NetworkStatus = reachAbility.currentReachabilityStatus()switch(wifiState){case NotReachable:item.serviceState = falsebreakdefault:break}case .restricted://受限制if(item.pstate != .stricted){item.pstate = .strictedself.stateAsyncUpdate?()}item.pstate = .strictedcase .notRestricted://Reachability库文中尾部放入//不受限制print("第一次进来也会显示这个状态,第一次进来需要")let reachAbility:Reachability = Reachability.forInternetConnection()let wifiState:NetworkStatus = reachAbility.currentReachabilityStatus()switch(wifiState){case NotReachable:item.serviceState = falsebreakdefault:break}if(item.pstate != .on){item.pstate = .on}self.stateAsyncUpdate?()item.pstate = .on@unknown default:print("")}

本地网络权限:

class LocalNetworkPermissionTester {var connection: NWConnectionvar success = falsevar semaphore: DispatchSemaphoreinit(semaphore: DispatchSemaphore) {self.semaphore = semaphorelet dispatchQueue = DispatchQueue(label: "LocalNetworkPermissionTester")self.connection = NWConnection(host: "127.255.255.255", port: 9, using: .udp)self.connection.stateUpdateHandler = { state inswitch state {case .ready:self.success = truesemaphore.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 = falsesemaphore.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 = .onlet semaphore = DispatchSemaphore(value: 0)let tester = LocalNetworkPermissionTester(semaphore: semaphore)semaphore.wait()print("success: \(tester.success)")if(tester.success){if(item.pstate != .on){item.pstate = .onself.stateAsyncUpdate?()}item.pstate = .on}else{if(item.pstate != .stricted){item.pstate = .strictedself.stateAsyncUpdate?()}item.pstate = .stricted}item.isNeedGet = true}
http://www.lryc.cn/news/395789.html

相关文章:

  • 用LangGraph、 Ollama,构建个人的 AI Agent
  • ubuntu20.04系统编译yolov8-obb.cpp代码记录
  • JavaScript的数组与函数
  • opencv--把cv::Mat数据转为二进制数据的保存和读取
  • 【微信小程序开发实战项目】——个人中心页面的制作
  • 基于MCU平台的HMI开发的性能优化与实战(下)
  • 评估测试用例有效性 5个方面
  • CentOS 7.9 快速更换 阿里云源教程
  • Python 编程快速上手——让繁琐工作自动化(第2版)读书笔记01 Python基础快速过关
  • 实战 | YOLOv8使用TensorRT加速推理教程(步骤 + 代码)
  • 绝区陆--大语言模型的幻觉问题是如何推动科学创新
  • 集训 Day 2 模拟赛总结
  • Linux系统(CentOS)安装Mysql5.7.x
  • YModem在Android上的实现
  • 循环练习题
  • Seata解决分布式事务
  • C语言编译报错error: expected specifier-qualifier-list before
  • 无缝协作:如何实现VMware与Ubuntu虚拟机的剪切板共享!
  • linux 进程堆栈分析
  • 【续集】Java之父的退休之旅:从软件殿堂到多彩人生的探索
  • LVS+Nginx高可用集群---Nginx进阶与实战
  • Appium环境搭建,华为nova8鸿蒙系统(包括环境安装,环境配置)(一)
  • 【React】React18 Hooks 之 useReducer
  • 【cocos creator】2.4.x实现简单3d功能,点击选中,旋转,材质修改,透明材质
  • Android EditText+ListPopupWindow实现可编辑的下拉列表
  • dify/api/models/task.py文件中的数据表
  • hdu物联网硬件实验3 按键和中断
  • pytorch通过 tensorboardX 调用 Tensorboard 进行可视化
  • linux查看目录下的文件夹命令,find 查找某个目录,但是不包括这个目录本身?
  • 单一设备上的 2 级自动驾驶:深入探究 Openpilot 的奥秘