Advertisement
conath

Copy files from iOS app bundle to documents directory

May 27th, 2020
1,763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.21 KB | None | 0 0
  1. /// Asyncronously copies images from exampleImages.bundle to the documents directory and reveals them in the document browser.
  2.     private func prepareExampleImages() {
  3.         DispatchQueue.global(qos: .userInitiated).async {
  4.             guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
  5.                 fatalError("Can't get URL for FileManager.SearchPathDirectory.documentDirectory")
  6.             }
  7.             guard let exampleImageBundlePath = Bundle.main.path(forResource: "exampleImages", ofType: "bundle") else {
  8.                 fatalError("Example image bundle missing or unreadable.")
  9.             }
  10.             let fileManager = FileManager.default
  11.             guard let filenames = try? fileManager.contentsOfDirectory(atPath: exampleImageBundlePath), filenames.count == 3 else {
  12.                 fatalError("Expected three example images in exampleImages.bundle")
  13.             }
  14.             var destinationURLs = [URL]()
  15.             for filename in filenames {
  16.                 let imageURL = URL(fileURLWithPath: "\(exampleImageBundlePath)/\(filename)")
  17.                 let destinationURL = documentsDirectory.appendingPathComponent(filename)
  18.                 destinationURLs.append(destinationURL)
  19.                 do {
  20.                     try fileManager.copyItem(at: imageURL, to: destinationURL)
  21.                 } catch {
  22.                     // file exists error: running tutorial with files of this name already present
  23.                     //  that's ok, we don't want to overwrite user data, skip this file
  24.                     //  In some locales, the error.localizedDescription fails to load, so the error code is returned instead.
  25.                     if error.localizedDescription.contains("exist") || error.localizedDescription.contains("ERR516") {
  26.                         // skip this file
  27.                         continue
  28.                     } else {
  29.                         // unknown errors are not handled; skip file
  30.                         // TODO unified error handling show the user a message about example image not loading
  31.                         assertionFailure("Unknown error while copying image from example bundle to documents directory!")
  32.                         continue
  33.                     }
  34.                 }
  35.             }
  36.             self.exampleImageURLs = destinationURLs
  37.             // tell document controller to reveal the example images, which updates last used date
  38.             // means if we do it in reverse order they will show up in the correct order in Recents tab (most recently revealed first)
  39.             destinationURLs = destinationURLs.reversed()
  40.             // delay to make sure the calls arrive at the remote view controller in this specific order
  41.             var delay = 0.0
  42.             for url in destinationURLs {
  43.                 DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
  44.                     self.presenterVC.documentBrowserVC.revealDocument(at: url, importIfNeeded: true) { (url, error) in
  45.                         // unknown error TODO universal user-facing error handling
  46.                         assert(error == nil, "UIDocumentBrowserViewController can't reveal the image! \(error!.localizedDescription)")
  47.                     }
  48.                 }
  49.                 delay += 0.2
  50.             }
  51.         }
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement