-
Notifications
You must be signed in to change notification settings - Fork 9
/
ViewController.swift
82 lines (72 loc) · 2.51 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// ViewController.swift
// Check-WiFI
//
// Created by ERU on 2019/06/12.
// Copyright © 2019 HackingGate. All rights reserved.
//
import UIKit
import SystemConfiguration.CaptiveNetwork
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentNetworkInfos: Array<NetworkInfo>? {
get {
return SSID.fetchNetworkInfo()
}
}
@IBOutlet weak var ssidLabel: UILabel!
@IBOutlet weak var bssidLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
updateWiFi()
} else {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
} else {
updateWiFi()
}
}
func updateWiFi() {
print("SSID: \(currentNetworkInfos?.first?.ssid ?? "")")
ssidLabel.text = currentNetworkInfos?.first?.ssid
bssidLabel.text = currentNetworkInfos?.first?.bssid
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
updateWiFi()
}
}
}
public class SSID {
class func fetchNetworkInfo() -> [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?
}