检测正确的USB设备是否连接 - Swift
使用最新的SwiftUI和Xcode 13,具有OS 12.3+的目标,我需要检测到特定的USB驱动器是否连接到机器。如果是,我需要将变量设置为true,如果将其删除为false。
我还没有找到任何更新的代码示例来执行此操作,而且Apple的文档是... Apple的文档。
我将其与以下代码一起使用,该代码将信息打印到控制台。它仅在指定的volumeUID上寻找USB设备,但是由于它确实是旧的C代码,我希望有人可以共享一种更新的方法,这也可以让我设置一个变量,而不是仅仅打印到安慰。
import DiskArbitration
import Foundation
import IOKit
class USB_Helper: ObservableObject {
@Published var usbConnected: Bool = false
init() {
print("USB Helper Initialized")
tryUSB()
}
func tryUSB() {
let validation: CFDictionary = [
"DADeviceProtocol":"USB",
"DAVolumeUUID": CFUUIDCreateFromString(kCFAllocatorDefault, "F0672BD0-1FA9-3567-452F-322138E12C35" as CFString)!
] as CFDictionary
let diskAppearedCallback: DADiskAppearedCallback = {
disk,
context in print(
"Disk appeared:",
disk
)
printDiskInfo(
disk
)
}
let diskDisappearedCallback: DADiskDisappearedCallback = {
disk,
context in print(
"Disk disappeared:",
disk
)
printDiskInfo(
disk
)
}
let runloop = RunLoop.current
let session = DASessionCreate(kCFAllocatorDefault)!
func printDiskInfo(_ disk: DADisk) {
guard let description = DADiskCopyDescription(disk) as? [String: Any] else {
fatalError()
}
print(description)
}
DARegisterDiskAppearedCallback(
session,
validation,
diskAppearedCallback,
nil
)
DARegisterDiskDisappearedCallback(
session,
validation,
diskDisappearedCallback,
nil
)
DASessionScheduleWithRunLoop(
session,
runloop.getCFRunLoop(),
CFRunLoopMode.defaultMode.rawValue
)
} // End Func
} // End Class
Using the latest SwiftUI and XCode 13, with a target of OS 12.3+ I need to detect whether or not a specific USB drive is connected to the machine. If it is I need to set a variable to true, if it's removed set that var to false.
I have yet to find any sort of updated code examples to do this, and Apple's docs are, well... Apple's docs.
I have it working with the following code, which prints info to the console. It only looks for USB devices with the VolumeUUID specified, but as it's really old C code, I'm hoping someone can share a much more updated way of doing this, which will also allow me to set a variable instead of just printing to the console.
import DiskArbitration
import Foundation
import IOKit
class USB_Helper: ObservableObject {
@Published var usbConnected: Bool = false
init() {
print("USB Helper Initialized")
tryUSB()
}
func tryUSB() {
let validation: CFDictionary = [
"DADeviceProtocol":"USB",
"DAVolumeUUID": CFUUIDCreateFromString(kCFAllocatorDefault, "F0672BD0-1FA9-3567-452F-322138E12C35" as CFString)!
] as CFDictionary
let diskAppearedCallback: DADiskAppearedCallback = {
disk,
context in print(
"Disk appeared:",
disk
)
printDiskInfo(
disk
)
}
let diskDisappearedCallback: DADiskDisappearedCallback = {
disk,
context in print(
"Disk disappeared:",
disk
)
printDiskInfo(
disk
)
}
let runloop = RunLoop.current
let session = DASessionCreate(kCFAllocatorDefault)!
func printDiskInfo(_ disk: DADisk) {
guard let description = DADiskCopyDescription(disk) as? [String: Any] else {
fatalError()
}
print(description)
}
DARegisterDiskAppearedCallback(
session,
validation,
diskAppearedCallback,
nil
)
DARegisterDiskDisappearedCallback(
session,
validation,
diskDisappearedCallback,
nil
)
DASessionScheduleWithRunLoop(
session,
runloop.getCFRunLoop(),
CFRunLoopMode.defaultMode.rawValue
)
} // End Func
} // End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是,似乎没有办法从回调内设置变量,但是解决方案非常简单。
以下是要设置变量的函数:
以下是通知对回调的修改:
i真的希望这篇文章对他人有所帮助,因为我花了很长时间才能弄清楚这一点。祝你好运!
The answer is that there seems to be no way to set the variable from within the callback, however the solution is rather simple.
Here are the functions to set the variable:
Here are the modifications to the callbacks with the notifications:
I really hope this post helps others because it took me a long time to figure it out. Good luck!