0

User picks document from UIDocumentPickerViewController that i will be working with later. Document picker delegate calls and gives me url for file, but there is no file at all.

This is how i create documentPicker. I use supportedFiles because typing manually extension doesn't work for me

let supportedFiles: [UTType] = [UTType.data]
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedFiles)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .fullScreen
present(documentPicker, animated: true, completion: nil)

There is documentPicker delegate with all checks

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

        var path = urls.first!.path
        let stream = InputStream(url: URL(fileURLWithPath: path))
        print(path)
        print(FileManager.default.fileExists(atPath: path))

        do {
            let items = try FileManager.default.contentsOfDirectory(atPath: path)
            print(items.count)
            for item in items {
                print("Found (item)")
            }
        } catch {
            print(error)
        }

        do {
            let csv = try CSVReader(stream: stream!)
            print("Everything is ok")
            while let row = csv.next() {
                print(row)
            }
        } catch {
            print(error)
        }
    }

And console show me this

/private/var/mobile/Containers/Shared/AppGroup/3232A257-B8F6-4F39-A12B-A7192EBF9524/File Provider Storage/Games.csv
false
Error Domain=NSCocoaErrorDomain Code=256 "The file “Games.csv” couldn’t be opened." UserInfo={NSUserStringVariant=(
    Folder
), NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/3232A257-B8F6-4F39-A12B-A7192EBF9524/File Provider Storage/Games.csv, NSUnderlyingError=0x282a277e0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}
cannotOpenFile

As i understand i got url for file that does not exists? Then why fileManager gives me an error that this file is not a directory instead of saying that there is nothing at this url? There was also an error that i dont have permission to read this file, so i changed it to be readable. That means that it can see it, but it can't? I just dont understand.

Also tried to change path by deleting /private but it didnt work

Update:

When trying to get list of items in folder in which Games.svc is located I get another error

Error Domain=NSCocoaErrorDomain Code=257 "The file “File Provider Storage” couldn’t be opened because you don’t have permission to view it." UserInfo={NSUserStringVariant=(
    Folder
), NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/3232A257-B8F6-4F39-A12B-A7192EBF9524/File Provider Storage/, NSUnderlyingError=0x2819254d0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
dandand
  • 3
  • 1
  • 4
  • "And console show me this" What is 'this'? You have eight lines with `print()`. – El Tomato Jul 28 '21 at 20:24
  • @ElTomato right after words "And console show me this" i wrote result in my console – dandand Jul 28 '21 at 20:55
  • Oh, okay, sorry about that. So you get 'this' as a result of which line? – El Tomato Jul 28 '21 at 21:01
  • @ElTomato From all of them. First line is the path. Second is file in this path exists. Third and fourth is the error i get from trying to open this file as a folder. Fifth line just an error from trying to decode this file. Also i will make an update on post right now – dandand Jul 28 '21 at 21:11
  • Whats the point of creating an url from the path of another url? Just use `urls.first` – Leo Dabus Jul 29 '21 at 01:30
  • `contentsOfDirectory`of a file path/url doesn't make any sense. What you need is to list its parent folder contents. Anyway I am not sure if you can get the contents of a directory outside your app bundle – Leo Dabus Jul 29 '21 at 01:33
  • @LeoDabus yes it doesnt make any sense. But this code gave me other type of error, so that is some progress for me. I also found that there is thing called sandboxing in Mac apps, that disables ability to look at files in disk. However sandboxing is missing in Ios apps but it is there. It can be disabled with entitlements.plist with commands like com.apple.private.security.no-sandbox com.apple.private.security.disk-device-access but i dont know where to write them, because there are no field for custom properties – dandand Jul 29 '21 at 10:02
  • @LeoDabus source: [link](https://www.reddit.com/r/jailbreakdevelopers/comments/l37ytr/error_domainnscocoaerrordomain_code257_the_file/gkbexv2?utm_source=share&utm_medium=web2x&context=3) – dandand Jul 29 '21 at 10:04

1 Answers1

-1

Found apple documentation about getting access to directories. Edited my code to this and now it is working

guard urls.first!.startAccessingSecurityScopedResource() else {
     print("Error getting access")
     return
}

defer { urls.first!.stopAccessingSecurityScopedResource() }

let path = urls.first!.path
let stream = InputStream(url: URL(fileURLWithPath: path))

do {
      let csv = try CSVReader(stream: stream!)
      print("Everything is ok")
      while let row = csv.next() {
      print(row)
}
      URL(fileURLWithPath: path).stopAccessingSecurityScopedResource()
} catch {
      print(error)
}

Basically before working with the URL you get the request to use secured URL by this function

guard urls.first!.startAccessingSecurityScopedResource() else {
     print("Error getting access")
     return
}

defer { urls.first!.stopAccessingSecurityScopedResource() }

And end working with secured connection by writing this line

URL(fileURLWithPath: path).stopAccessingSecurityScopedResource()

However code written in documentation is not working for me, and error check(that i deleted) still giving me an error

Update

If code is not working, you can delete this line

defer { urls.first!.stopAccessingSecurityScopedResource() }
dandand
  • 3
  • 1
  • 4
  • If this is not the answer, don't put it in the Answer field. – matt Jul 29 '21 at 13:03
  • @matt this is the answer – dandand Jul 29 '21 at 14:54
  • Then what is the stuff about "not working for me" and "giving me an error" about? – matt Jul 29 '21 at 15:00
  • Also, since the documentation is quite clear that the URL provided by a document picker is security scoped and must be treated accordingly, it isn't clear how this question and answer is useful. Basically aren't you simply saying that you've recently discovered something that is already generally well known? – matt Jul 29 '21 at 15:04
  • @matt it is about recommended code in Apple documentation, not about my code. My code working fine – dandand Jul 29 '21 at 15:05
  • OK well maybe you could clarify that. – matt Jul 29 '21 at 15:06
  • @matt 'However code written in DOCUMENTATION is not working for me, and error check(that i deleted) still giving me an error' and also 'Edited my code to this and now it is working' so everything is fine – dandand Jul 29 '21 at 15:09