0

I am having trouble finding where to place files on my Mac so that my Xcode simulator sees them.

Working on a "file upload" section for my app. Before I call the UIDocumentPickerViewController, I do call my own function printSimDir which I use to open the proper folder on my Mac so I can throw my files in there.

And in there I have three files: "blank_inv, invoice_001.cvs, and example.mp3"

However, in my simulator, I don't see these files. I do however keep seeing one xls file that is not any of three above files. So at one point I did get this right. But not anymore.

I realize that my problem might also be in how I am calling the UIDocumentPickerViewController so am including that code as well.

case ButtType.file.rawValue:
    printSimDir()

    let supportedTypes: [UTType] = [UTType.spreadsheet, UTType.commaSeparatedText, .mp3]
    let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes)
    pickerViewController.delegate = self
    pickerViewController.allowsMultipleSelection = false
    present(pickerViewController, animated: true, completion: nil)


...


extension UploadInv:  UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    
    for url in urls {
        guard url.startAccessingSecurityScopedResource() else {
            print ("error")
            return
        }
        
        xFile = XFile(fileUrl: url, key: "filename")
        myStartUPButt.isEnabled = true
        
        do { url.stopAccessingSecurityScopedResource() }
        myStatus.text = xFile?.filename
    }
}

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
    controller.dismiss(animated: true, completion: nil)
    }
}   

printSimDir()

func printSimDir(){
    //  tried the commented code as well
    //  let fManager = FileManager.default
    //  guard let url = fManager.urls(for: .documentDirectory, in: .userDomainMask).first else {return}
    //  print ("\(url)")
    
    #if targetEnvironment(simulator)
    if let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.path {
        print("Documents Directory: \(documentsPath)")
    }
    #endif
}
Willeke
  • 14,578
  • 4
  • 19
  • 47
Bartender1382
  • 195
  • 1
  • 10
  • Document picker view controller will copy the selected files (from outside your bundle) to a temporary directory. It has no relation with your documents directory located inside your app – Leo Dabus Apr 16 '22 at 19:13
  • @LeoDabus Not sure I understand. The document picker works fine on my iPhone. I see all possible files. But easier to do certain debugging on the simulator. So am I looking at the wrong directory in my printSimDir() function? – Bartender1382 Apr 16 '22 at 19:19

1 Answers1

0

For those who come across this. Not sure where I found the answer, but it's quite simple.

Simply drag and drop the files you want from your Mac to the Xcode simulator's Home Screen. Currently have only tried them one at a time.

Is this the proper answer? I do not know. But it does work.

Haven't tried every way to do this, (one at a time, multiple files at once, multiple simulators, etc.) But even after a Mac restart the files are still there.

Bartender1382
  • 195
  • 1
  • 10