Can't save file on iOS

Hello all!
I've killed a few days trying to save a file and list it in a home directory.

First I retrieve a home directory URL.
I tried several options:
Code Block
docDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
docDir = URL(fileURLWithPath: NSHomeDirectory())

In any way when running on simulator or my iPhone it results in just "file:///"
Well, maybe that's ok for sandboxed iOS app...

After that I prepare a file URL. I tried two ways:
Code Block
let file = docDir.appendingPathComponent("myfile.tmp")
let file = URL(fileURLWithPath: "myfile.tmp")
print(file.absoluteURL)

Here I get "file:///myfile.tmp"

Finally I try to create file
Code Block
        do {
            try  Data(count: 0).write(to: file2)
        }
        catch {
            print("Failed to create file: '\(file)' because of: \(error)")
        }

And here is what I get

On simulator:

Failed to create file: './myfile.tmp -- file:///' because of: Error Domain=NSCocoaErrorDomain Code=642 "You can’t save the file “myfile.tmp” because the volume “AirHD — данные” is read only." UserInfo={NSFilePath=/myfile.tmp, NSUnderlyingError=0x600000dea1f0 {Error Domain=NSPOSIXErrorDomain Code=30 "Read-only file system"}}

On iPhone:

Failed to create file: './myfile.tmp -- file:///' because of: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “myfile.tmp” in the folder “System”." UserInfo={NSFilePath=/myfile.tmp, NSUnderlyingError=0x281725a40 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

But why in the folder “System” ??
It should have been my Documents folder!
This works for me:

        let file = "file5.txt"
        let text = "some text"
        let fm = FileManager.default
        let dir = fm.urls(for: .documentDirectory, in: .userDomainMask)
        if dir.count != 0  {
            let log = dir[0].appendingPathComponent(file)
            print(dir[0])
            do {
                try text.write(to: log, atomically: true, encoding: .ascii)
            }
            catch {
                print("error writing file")
            }
        }
        else {
            print("document map in local domain not found")
            exit(0)
        }
Can't save file on iOS
 
 
Q