Swift 4 Version:
Deklarieren NSWorkspace in applicationDidFinishLaunching und fügen Beobachter für mount und unmount Veranstaltungen.
let workspace = NSWorkspace.shared
workspace.notificationCenter.addObserver(self, selector: #selector(didMount(_:)), name: NSWorkspace.didMountNotification, object: nil)
workspace.notificationCenter.addObserver(self, selector: #selector(didUnMount(_:)), name: NSWorkspace.didUnmountNotification, object: nil)
Erfassung ein- und aushängen Veranstaltungen in:
@objc func didMount(_ notification: NSNotification) {
if let devicePath = notification.userInfo!["NSDevicePath"] as? String {
print(devicePath)
}
}
@objc func didUnMount(_ notification: NSNotification) {
if let devicePath = notification.userInfo!["NSDevicePath"] as? String {
print(devicePath)
}
}
Es Gerätepfad drucken z/Volumes/EOS_DIGITAL Hier sind die Konstanten, die Sie von userinfo lesen kann.
NSDevicePath,
NSWorkspaceVolumeLocalizedNameKey
NSWorkspaceVolumeURLKey
Gibt es einen Grund, diese über NSWorkspace wie unten beschrieben zu verwenden? Es ist eine 3rd-Party-Bibliothek, also müssten die Vorteile erheblich sein, um mich davon zu überzeugen, dem Projekt eine zusätzliche Abhängigkeit hinzuzufügen. – Brian