mirror of
https://github.com/tappollo/OSCKit.git
synced 2026-03-26 06:35:17 +08:00
Add SSID tracking
This commit is contained in:
@@ -24,17 +24,26 @@
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="Kaz-P4-lJh">
|
||||
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
|
||||
</imageView>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VZf-MU-5Pk">
|
||||
<rect key="frame" x="166" y="619" width="42" height="21"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="17"/>
|
||||
<nil key="textColor"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
</subviews>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<constraints>
|
||||
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="Kaz-P4-lJh" secondAttribute="bottom" id="MPe-zE-dFp"/>
|
||||
<constraint firstItem="Kaz-P4-lJh" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="VM8-J8-hSO"/>
|
||||
<constraint firstAttribute="trailing" secondItem="Kaz-P4-lJh" secondAttribute="trailing" id="aWi-oy-Xv2"/>
|
||||
<constraint firstItem="VZf-MU-5Pk" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="oIW-pj-u3q"/>
|
||||
<constraint firstItem="Kaz-P4-lJh" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="yoU-kH-bou"/>
|
||||
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="VZf-MU-5Pk" secondAttribute="bottom" constant="27" id="zax-Zk-Daz"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<connections>
|
||||
<outlet property="image" destination="Kaz-P4-lJh" id="OyD-J2-cEG"/>
|
||||
<outlet property="ssidLabel" destination="VZf-MU-5Pk" id="cQ9-Tj-0xv"/>
|
||||
</connections>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
||||
|
||||
@@ -10,8 +10,11 @@ import UIKit
|
||||
import OSC
|
||||
|
||||
class ViewController: UIViewController {
|
||||
@IBOutlet weak var ssidLabel: UILabel!
|
||||
@IBOutlet weak var image: UIImageView!
|
||||
|
||||
var ssidObservationCancellation: (() -> Void)?
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
@@ -20,8 +23,17 @@ class ViewController: UIViewController {
|
||||
self.image.image = image
|
||||
}
|
||||
|
||||
ssidObservationCancellation = OSCKit.shared.ssid {[weak self] (ssid) in
|
||||
self?.ssidLabel.text = ssid
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.ssidObservationCancellation?()
|
||||
}
|
||||
|
||||
|
||||
override func didReceiveMemoryWarning() {
|
||||
super.didReceiveMemoryWarning()
|
||||
// Dispose of any resources that can be recreated.
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
// Copyright © 2017 Tappollo Inc. All rights reserved.
|
||||
//
|
||||
import SwiftyyJSON
|
||||
import SystemConfiguration.CaptiveNetwork
|
||||
|
||||
public class OSCKit {
|
||||
public static let shared = OSCKit()
|
||||
@@ -22,23 +21,5 @@ public class OSCKit {
|
||||
|
||||
private init() { }
|
||||
|
||||
public func isConnectedToDeviceWiFi(withPrefix prefix: String) -> Bool {
|
||||
return SSID.current?.hasPrefix(prefix) == true
|
||||
}
|
||||
}
|
||||
|
||||
struct SSID {
|
||||
static var current: String? {
|
||||
if let interfaces = CNCopySupportedInterfaces() {
|
||||
for i in 0..<CFArrayGetCount(interfaces) {
|
||||
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
|
||||
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
|
||||
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
|
||||
if let interfaceData: NSDictionary = unsafeInterfaceData, let ssid = interfaceData["SSID"] as? String {
|
||||
return ssid
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
81
Source/SSID.swift
Normal file
81
Source/SSID.swift
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// SSID.swift
|
||||
// Pods
|
||||
//
|
||||
// Created by Zhigang Fang on 5/15/17.
|
||||
//
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SystemConfiguration.CaptiveNetwork
|
||||
|
||||
final class SSID: NSObject {
|
||||
|
||||
static let shared = SSID()
|
||||
|
||||
private var subscribers: [String: (String?) -> Void] = [:] {
|
||||
didSet {
|
||||
if subscribers.count > 0 {
|
||||
self.startTracking()
|
||||
} else {
|
||||
self.stopTracking()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var previousSSID: String?
|
||||
|
||||
private var ssidTrackingTimer: Timer?
|
||||
|
||||
private func startTracking() {
|
||||
guard ssidTrackingTimer == nil else { return }
|
||||
self.ssidTrackingTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timerUpdated), userInfo: nil, repeats: true)
|
||||
self.ssidTrackingTimer?.fire()
|
||||
}
|
||||
|
||||
private dynamic func timerUpdated() {
|
||||
let currentSSID = self.current
|
||||
if self.previousSSID != currentSSID {
|
||||
self.previousSSID = currentSSID
|
||||
self.subscribers.forEach({ (pair) in
|
||||
pair.value(currentSSID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private func stopTracking() {
|
||||
self.ssidTrackingTimer?.invalidate()
|
||||
self.ssidTrackingTimer = nil
|
||||
}
|
||||
|
||||
func subscribe(onChange subscriber: @escaping (String?) -> Void) -> () -> Void {
|
||||
let uuid = UUID().uuidString
|
||||
subscribers[uuid] = subscriber
|
||||
return {
|
||||
self.subscribers[uuid] = nil
|
||||
}
|
||||
}
|
||||
|
||||
private var current: String? {
|
||||
if let interfaces = CNCopySupportedInterfaces() {
|
||||
for i in 0..<CFArrayGetCount(interfaces) {
|
||||
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
|
||||
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
|
||||
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
|
||||
if let interfaceData: NSDictionary = unsafeInterfaceData, let ssid = interfaceData["SSID"] as? String {
|
||||
return ssid
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension OSCKit {
|
||||
|
||||
public func ssid(changed: @escaping (String?) -> Void) -> () -> Void {
|
||||
return SSID.shared.subscribe(onChange: changed)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user