how can I read a file out of sandbox? (swift 5 , xcode 12)

814 Views Asked by At

I need to send a file to the server. I select this file with UIViewControllerRepresentable which provides the url of the file and every time I try to upload I get the same message :- Domain=NSCocoaErrorDomain Code=257 "The file “example1.xml” couldn’t be opened because you don’t have permission to view it." . I think I need to add permissions on info.plist but I don't find the right one. If I select a file in the sandbox it works.

1

There are 1 best solutions below

0
Vic On

I solved it by using .startAccessingSecurityScopedResource () on the URL. Eg:

`var path = URL (string: "pathFile")!

//before reading the file

path.startAccessingSecurityScopedResource ()

//at the end

path.stopAccessingSecurityScopedResource ()

//to close. `

resources https://developer.apple.com/documentation/foundation/nsurl/1417051-startaccessingsecurityscopedreso