ios - 多人连接 wifi 和蓝牙关闭
问题描述
iOS中有什么方法可以检测Wifi和蓝牙是否关闭,以便我可以向用户显示警报以从设置中启用它。
注意:我感兴趣的是硬件设置是开/关,而不是互联网是否可以访问。
解决方案
//对于 Wifi 使用 SwiftReachability https://github.com/ashleymills/Reachability.swift
//声明这个属性不会超出你的监听器的范围
let reachability = try! Reachability()
//declare this inside of viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .unavailable:
print("Network not reachable")
}
}
停止通知
reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
// 蓝牙导入 CoreBluetooth
var myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
//BT管理器
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
{
if peripheral.state == .poweredOn {
// myBTManager!.startAdvertising(_broadcastBeaconDict)
} else if peripheral.state == .poweredOff {
// myBTManager!.stopAdvertising()
} else if peripheral.state == .unsupported
{
} else if peripheral.state == .unauthorized
{
}
}
推荐阅读
- shell - 用于显示给定时间段内最多命中的 Shell 命令
- java - 爪哇。Foreach:泛型类中的不兼容类型
- batch-file - 从任务计划程序打开和关闭 windows10 中的虚拟机
- android - Android:第一次失败后如何停止仪器测试
- ios - 可以分发一个未发布的 react-native ios 应用程序吗?
- json - 文件名太长错误在 bash 中创建 json 文件
- javascript - 删除html表中的空行
- html - 使用 URL 启动 Facebook 应用程序 iframe 中的链接?
- kotlin - 在 CordaR3 交易中,我可以使用只有一方知道的状态吗?
- r - 带有操作按钮的闪亮删除选项卡