Code Monkey home page Code Monkey logo

diskmonitor's Introduction

DiskMonitor

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.

Usage

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.

Installation

Swift Package Manager

  1. Go to your project settings
  2. Tap on your Project name above listed targets
  3. Open tab swift packages
  4. Add DiskMonitor's repo as a package.

Further Information

If you find DiskMonitor Package helpful, you might as well like DiskUtil.

Licence

The framework is licensed under MIT licence. For more information see file LICENCE

diskmonitor's People

Contributors

stoqn4opm avatar

Stargazers

 avatar

Watchers

 avatar  avatar

diskmonitor's Issues

Sample project?

Hello, I am trying to use this library combined with the DiskUtil library to do the following:

When a USB Drive is inserted do;

  1. Tell the User
  2. Ask for confirmation
  3. Format it

However, I cannot see how I get the name and where I parse it into, also I cannot get Diskmonitor to show notifications.

Is it possible you could make a demo project, thank you :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.