Hey, I am trying to save the url which I get from my didPickDocumentAt as a security scoped bookmark
This is my code:
Here just the declaration of the UIDocumentPickerViewController
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: .open)
documentPicker.delegate = self
if #available(iOS 11.0, *)
{
documentPicker.allowsMultipleSelection = false
}
else
{
// Fallback on earlier versions
}
present(documentPicker, animated: true, completion: nil)
Now didPickDocumentAt - there I am saving my bookmark data from my selected file on the smb server. Data.bookmarkAdmin is just the variable in which I store the bookmark.
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
{
print(url)
do
{
let shouldStoppAccessing = url.startAccessingSecurityScopedResource()
defer
{
if shouldStoppAccessing
{
url.stopAccessingSecurityScopedResource()
}
}
let bookmarkData: Foundation.Data = try url.bookmarkData()
Data.bookmarkAdmin = bookmarkData
print(Data.bookmarkAdmin)
}
catch let error
{
print(error)
}
}
Later in my app I want to use the bookmark data to create the url again and write on that file
func readFile()
{
Admin.fileContent = ""
Admin.readString.removeAll()
do
{
var isStale = false
let url = try URL(resolvingBookmarkData: Data.bookmarkAdmin, bookmarkDataIsStale: &isStale)
guard !isStale
else
{
return
}
var errorText: Error?
do
{
let didStartAccessing = url.startAccessingSecurityScopedResource()
defer
{
if didStartAccessing
{
url.stopAccessingSecurityScopedResource()
}
}
print(url)
let fileURLs = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
Admin.fileContent = try String(contentsOf: fileURLs[0], encoding: .utf8)
print("ADMIN: \(Admin.fileContent)")
}
catch
{
errorText = error
}
// Errorhandling
if errorText != nil
{
let alertController = UIAlertController(title: "Error", message: "Keine Datei gefunden \(String(describing: errorText))", preferredStyle: .alert)
let okAction = UIAlertAction(title: "ok", style: .default, handler: nil)
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}
else {}
}
catch let error {}
}
I get an error in my readFile() function ERROR:
Error Domain=NSCocoaErrorDomain Code=256 "The file “testaufbau” couldn’t be opened." UserInfo={NSURL=file:///private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/ctPrTwTestApp/testaufbau.csv, NSFilePath=/private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/ctPrTwTestApp/testaufbau.csv, NSUnderlyingError=0x2834bfbd0 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}
When I set the url manually in the readFile() without the bookmark just
let url: URL = URL(fileURLWithPath: "/private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/ctPrTwTestApp/testaufbau.csv")
Then I get a different ERROR:
Error Domain=NSCocoaErrorDomain Code=257 "The file “testaufbau.csv” couldn’t be opened because you don’t have permission to view it." UserInfo={NSURL=file:///private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/ctPrTwTestApp/testaufbau.csv, NSFilePath=/private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/ctPrTwTestApp/testaufbau.csv, NSUnderlyingError=0x280ac1440 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
How can I solve this and how do I get permission?
Look at the doc.
It exists, but you have to specify options:
init(contentsOf: URL, options: Data.ReadingOptions)
That's for smb access ? Didn't eskimo answer the question here: https://forums.developer.apple.com/message/415231#415231