1、开发者账号里面的id,需要勾选WiFi
2、权限文件需要添加Access WiFi Information为true
3、开启定位信息
4、获取WiFi信息
附注:
WechatIMG3444.png
WechatIMG3445.png
// MARK: 设置定位
@objc func configLocationAction() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
let status: CLAuthorizationStatus?
if #available(iOS 14.0, *) {
status = self.locationManager.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
if status == .notDetermined {
// 尚未设置定位权限
self.locationManager.requestWhenInUseAuthorization()
} else if status == .denied || status == .restricted {
// 拒绝权限
self.flag1 = 1
self.updateUIAction()
} else {
// 其他就当已授权
self.locationManager.requestLocation()
}
}
// MARK: 定位代理方法
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
// 定位权限改动
self.configLocationAction()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
// 转换地名
print(location)
// 停止定位
self.locationManager.stopUpdatingLocation()
// 开始搜索WiFi设备
getWiFiName()
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// 出现错误
print("定位失败")
self.flag1 = 1
self.updateUIAction()
}
// MARK: 获取当前设备连接的WiFi
static func getWiFiName() -> [NetworkInfo]? {
if let interfaces: NSArray = CNCopySupportedInterfaces() {
var networkInfos = [NetworkInfo]()
for interface in interfaces {
let interfaceName = interface as! String
var networkInfo = NetworkInfo(interface: interfaceName,
success: false,
ssid: nil,
bssid: nil)
if let dict = CNCopyCurrentNetworkInfo(interfaceName as CFString) as NSDictionary? {
networkInfo.success = true
networkInfo.ssid = dict[kCNNetworkInfoKeySSID as String] as? String
networkInfo.bssid = dict[kCNNetworkInfoKeyBSSID as String] as? String
}
networkInfos.append(networkInfo)
}
return networkInfos
}
return nil
}
struct NetworkInfo {
var interface: String
var success: Bool = false
var ssid: String?
var bssid: String?
}
结果打印
<+22.57086472,+113.87180399> +/- 65.00m (speed -1.00 mps / course -1.00) @ 2022/3/26 中国标准时间 11:56:15
Optional([AIBOProject.Tools.NetworkInfo(interface: "en0", success: true, ssid: Optional("UICYC"), bssid: Optional("80:ea:7:26:14:60"))])
网友评论