-1

im trying to eject/unmount a usb drive in swift i think i have to use fileManager.unmountVolume

let urlString = "/Volumes/UNTITLED/".addingPercentEncoding(withAllowedCharacters: .urlUserAllowed)
let url = URL(string: urlString!)

fileManager.unmountVolume(at: url!, options: FileManager.UnmountOptions.init(), completionHandler: {(_) in})

wen no drive is plugged in i get no output but wen a drive is plugged in i get CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jeana
  • 11
  • 2

1 Answers1

0

urlString isn't a URL string -- it's a filesystem path. Creating a URL from it using URL(string:) results in an incomplete URL.

Use the URL(fileUrlWithPath:) constructor instead to create a file:// URL. (You don't need to percent-encode a path before passing it to this function.)

  • thank you, just to be clear this code is correct? `fileManager.unmountVolume(at: URL(fileURLWithPath: "/Volumes/UNTITLED"), options: FileManager.UnmountOptions.init(), completionHandler: {(_) in})` – jeana Apr 03 '18 at 21:53