Swift 监视新连接的设备
我希望每当新设备连接到计算机(如移动设备或USB)时,我的macOS应用程序都会不断收到通知 我有下面的代码,我正试图得到下面的工作 它的问题是,在将Swift 监视新连接的设备,swift,cocoa,Swift,Cocoa,我希望每当新设备连接到计算机(如移动设备或USB)时,我的macOS应用程序都会不断收到通知 我有下面的代码,我正试图得到下面的工作 它的问题是,在将let example=example()添加到我的viewDidLoad后,它只在程序首次启动时调用函数一次。我怎样才能让它在程序启动后仍然检查设备 import Foundation import IOKit import IOKit.usb public protocol USBWatcherDelegate: class { //
let example=example()
添加到我的viewDidLoad后,它只在程序首次启动时调用函数一次。我怎样才能让它在程序启动后仍然检查设备
import Foundation
import IOKit
import IOKit.usb
public protocol USBWatcherDelegate: class {
/// Called on the main thread when a device is connected.
func deviceAdded(_ device: io_object_t)
/// Called on the main thread when a device is disconnected.
func deviceRemoved(_ device: io_object_t)
}
/// An object which observes USB devices added and removed from the system.
/// Abstracts away most of the ugliness of IOKit APIs.
public class USBWatcher {
private weak var delegate: USBWatcherDelegate?
private let notificationPort = IONotificationPortCreate(kIOMasterPortDefault)
private var addedIterator: io_iterator_t = 0
private var removedIterator: io_iterator_t = 0
public init(delegate: USBWatcherDelegate) {
self.delegate = delegate
func handleNotification(instance: UnsafeMutableRawPointer?, _ iterator: io_iterator_t) {
let watcher = Unmanaged<USBWatcher>.fromOpaque(instance!).takeUnretainedValue()
let handler: ((io_iterator_t) -> Void)?
switch iterator {
case watcher.addedIterator: handler = watcher.delegate?.deviceAdded
case watcher.removedIterator: handler = watcher.delegate?.deviceRemoved
default: assertionFailure("received unexpected IOIterator"); return
}
while case let device = IOIteratorNext(iterator), device != IO_OBJECT_NULL {
handler?(device)
IOObjectRelease(device)
}
}
let query = IOServiceMatching(kIOUSBDeviceClassName)
let opaqueSelf = Unmanaged.passUnretained(self).toOpaque()
// Watch for connected devices.
IOServiceAddMatchingNotification(
notificationPort, kIOMatchedNotification, query,
handleNotification, opaqueSelf, &addedIterator)
handleNotification(instance: opaqueSelf, addedIterator)
// Watch for disconnected devices.
IOServiceAddMatchingNotification(
notificationPort, kIOTerminatedNotification, query,
handleNotification, opaqueSelf, &removedIterator)
handleNotification(instance: opaqueSelf, removedIterator)
// Add the notification to the main run loop to receive future updates.
CFRunLoopAddSource(
CFRunLoopGetMain(),
IONotificationPortGetRunLoopSource(notificationPort).takeUnretainedValue(),
.commonModes)
}
deinit {
IOObjectRelease(addedIterator)
IOObjectRelease(removedIterator)
IONotificationPortDestroy(notificationPort)
}
}
extension io_object_t {
/// - Returns: The device's name.
func name() -> String? {
let buf = UnsafeMutablePointer<io_name_t>.allocate(capacity: 1)
defer { buf.deallocate(capacity: 1) }
return buf.withMemoryRebound(to: CChar.self, capacity: MemoryLayout<io_name_t>.size) {
if IORegistryEntryGetName(self, $0) == KERN_SUCCESS {
return String(cString: $0)
}
return nil
}
}
}
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class Example: USBWatcherDelegate {
private var usbWatcher: USBWatcher!
init() {
usbWatcher = USBWatcher(delegate: self)
}
func deviceAdded(_ device: io_object_t) {
print("device added: \(device.name() ?? "<unknown>")")
}
func deviceRemoved(_ device: io_object_t) {
print("device removed: \(device.name() ?? "<unknown>")")
}
}
<代码>导入基础
导入IOKit
导入IOKit.usb
公共协议USBWatcherDelegate:类{
///连接设备时在主线程上调用。
添加了功能设备(设备:io对象)
///当设备断开连接时在主线程上调用。
func设备已移动(设备:io对象)
}
///观察从系统中添加和删除的USB设备的对象。
///抽象掉了iokitapi的大部分丑陋之处。
公共级USBWatcher{
私有弱var委托:USBWatcherDelegate?
private let notificationPort=IONotificationPortCreate(kiomaterportdefault)
专用变量加法器:io_迭代器_t=0
私有var removedIterator:io_iterator_t=0
公共初始化(委托:USBWatcherDelegate){
self.delegate=委托
func handleNotification(实例:UnsafeMutableRawPointer?,iterator:io_iterator_t){
让watcher=Unmanaged.from不透明(实例!).takeUnrepainedValue()
let处理程序:((io迭代器)->Void)?
开关迭代器{
case watcher.addeditor:handler=watcher.delegate?.deviceAdded
case watcher.removedIterator:handler=watcher.delegate?.deviceRemoved
默认值:assertionFailure(“收到意外的IOIterator”);返回
}
而case let device=IOIteratorNext(迭代器),device!=IO\u OBJECT\u NULL{
处理程序?(设备)
IOObjectRelease(设备)
}
}
let query=IOServiceMatching(kIOUSBDeviceClassName)
让opaqueSelf=Unmanaged.passUnretained(self).toOpaque()
//注意连接的设备。
IOServiceAddMatchingNotification(
通知端口、kIOMatchedNotification、查询、,
手动提示、不透明和加法器)
handleNotification(实例:opaqueSelf,additerator)
//注意断开的设备。
IOServiceAddMatchingNotification(
通知端口,kIOTerminatedNotification,查询,
手动通知、不透明自身和移除迭代器)
handleNotification(实例:opaqueSelf、removedIterator)
//将通知添加到主运行循环以接收将来的更新。
CFRunLoopAddSource(
CFRunLoopGetMain(),
IONotificationPortGetRunLoopSource(notificationPort).takeUnrepainedValue(),
(通用模式)
}
脱硝{
IOObjectRelease(加法器)
IOObjectRelease(Remove迭代器)
IONotificationPortDestroy(通知端口)
}
}
扩展io_对象\u t{
///-返回:设备的名称。
func name()->字符串{
让buf=unsafemeutablepointer.allocate(容量:1)
延迟{buf.deallocate(容量:1)}
返回带MemoryRebound的基本单位(收件人:CChar.self,容量:MemoryLayout.size){
如果IORegistryEntryGetName(self,$0)=KERN\u SUCCESS{
返回字符串(cString:$0)
}
归零
}
}
}
导入PlaygroundSupport
PlaygroundPage.current.NeedsDefiniteExecution=true
类示例:USBWatcherDelegate{
私人var usbWatcher:usbWatcher!
init(){
usbWatcher=usbWatcher(代理:self)
}
添加了功能设备(设备:io对象){
打印(“添加的设备:\(device.name()??”))
}
func设备已移动(设备:io对象){
打印(“设备已删除:\(device.name()??”))
}
}
视图控制器应该是
USBWatcherDelegate
而不是示例。@Willeke我对此仍然有点困惑,您能告诉我到底需要更改什么吗?Classexample
只是一个示例。执行在viewcontroller中执行的示例
操作。USB watcher的代理应该是您的viewcontroller。viewcontroller应该是USBWatcherDelegate
,而不是示例。@Willeke我对此仍然有点困惑,您能告诉我到底需要更改什么吗?Classexample
只是一个示例。执行在viewcontroller中执行的示例
操作。USB watcher的代理应该是您的viewcontroller。