DiskMonitor is SwiftPM Package for monitoring added/ejected/renamed disks written in Swift 5.1, working using Apple's low level Disk Arbritration framework.
Build using Swift 5.1, XCode 11.3, supports macOS only.
The framework provide its functionality through the DiskMonitor
class. You should make an instance of this class using the
/// Creates a new disk monitor with an optional delegate passed.
///
/// `DiskMonitor` is a simple class that acts as a wrapper around the `DiskArbritation` framework from Apple that notifies
/// about changes with disk drives.
///
/// - Parameter delegate: The delegate object that you want notified in the case of new discoveries.
public init(withDelegate delegate: DiskMonitorDelegate? = nil)
initialiser. You can optionally pass delegate now or set the property later. Setting delegate is the way the framework communicates back when change occurs with the disks. The delegate
object should conform to:
/// Requirements for being a delegate to a `DiskMonitor`.
public protocol DiskMonitorDelegate: AnyObject {
/// Called when a raw disk appears.
///
/// - Parameters:
/// - monitor: The monitor that found it.
/// - diskAppeared: The raw `DADisk`.
func rawDiskMonitor(_ monitor: DiskMonitor, diskAppeared disk: DADisk)
/// Called when a raw disk disappears.
///
/// - Parameters:
/// - monitor: The monitor that found it.
/// - diskAppeared: The raw `DADisk`.
func rawDiskMonitor(_ monitor: DiskMonitor, diskDisappeared disk: DADisk)
/// Called when a raw disk was renamed.
///
/// - Parameters:
/// - monitor: The monitor that found it.
/// - diskAppeared: The raw `DADisk`.
func rawDiskMonitor(_ monitor: DiskMonitor, diskRenamed disk: DADisk)
}
Alternatively you can receive updates in the form of Notification
s in the Notification Center.default
center.
// MARK: - Notifications
extension Notification.Name {
/// Posted by the `DiskMonitor` when a disk appears. The notification's object is the raw `DADisk`.
public static let rawDiskAppeared = Notification.Name("DiskMonitor.rawDiskAppeared")
/// Posted by the `DiskMonitor` when a disk dissapears. The notification's object is the raw `DADisk`.
public static let rawDiskDisappeared = Notification.Name("DiskMonitor.rawDiskDisappeared")
/// Posted by the `DiskMonitor` when a disk gets renamed. The notification's object is the raw `DADisk`.
public static let rawDiskRenamed = Notification.Name("DiskMonitor.rawDiskRenamed")
}
After you have chosen your callback method, you are ready to call:
/// Makes the disk monitor start monitor what disks are added/ejected/renamed.
///
/// It posts notifications for adding/ejecting/renaming
/// `.rawDiskAppeared`/`.rawDiskDisappeared`/`.rawDiskRenamed`.
///
/// You can stop the monitoring process with `stopMonitoring()`.
public func startMonitoring()
and
/// Stops the currently launched disk monitoring.
///
/// You can start it again with `startMonitoring()`.
public func stopMonitoring()
when you want to end the monitoring process.
- Go to your project settings
- Tap on your Project name above listed targets
- Open tab swift packages
- Add DiskMonitor's repo as a package.
If you find DiskMonitor
Package helpful, you might as well like DiskUtil.
The framework is licensed under MIT licence. For more information see file LICENCE