"Open file error: No such file or directory" upon opening iCloud file

I'm struggling with writing to an iCloud file. I want my IOS app to open files on the iCloud driver. I get the above error upon return from fopen(filename, "w"). Further, the container name (iCloud.TrackInstructor) appears in red under the iCloud stanza of the Signings & Capabilities page.

I can see my container under the CloudKit page of my Apple Developer account. However, below, it states:

Error loading container details.

Also in red.

FWIW, my app is trying to open:

/private/var/mobile/Library/Mobile%20Documents/iCloud~TrackInstructor/Documents/nav_detailed_file_20220719_111330.csv

I have IOS 15.5 and Xcode 13.4.1.

I'm struggling. Help or ideas are appreciated.

I don't think you're supposed to be able to see iCloud Drive files in the CloudKit developer page. CloudKit != iCloud Drive. Have you enabled CloudKit instead of iCloud Drive somewhere?

You may need to create the Documents/ directory initially.

Are you using a coordinated write block?

WRT enabling CloudKit: I think the answer is yes as I checked both "iCloud Documents" and "CloudKit" under iCloud on my target's "Signing & Capabilities" page.

WRT directory creation: I did try to write my files into:

/private/var/mobile/Library/Mobile%20Documents/iCloud~TrackInstructor/

thus bypassing the need (I believe) for the Documents/ directory. How would I manually create the Documents/ directory?

I don't know what a coordinated write block is, so I assume the answer is "No." I assume the lack of a coordinate write block would affect the fopen() command as well as fwrite(). Correct?

BTW, I omitted from the original posting that I have updated the Info.plist file with 3 key/value pairs which I am told are required to write to the iCloud.

It there a particular naming convention for the container name? I may have incorrectly taken some liberties with the container name using something other than the project name. I don't know why I did that...

 How would I manually create the Documents/ directory?

In C, mkdir(path, 0777).

In C++, create_directory(path).

In objC, [[NSFileManager defaultManager] createDirectoryAtURL: [NSURL urlWithFileSystemRepresentation: path] withIntermediateDirectories: false attributes: @[Some@[Extra]VerboSity]here] error: everyonejustpassesnil_becausewe'vegivenup_bythispoint_butReallyWeShouldDoSomethingToHandleErrors]

In Swift, same as objC but with different punctuation.

I'm not sure if this is necessary. What does seem to be the case is that if you haven't done anything then the empty directory is not visible in e.g. the Files app. I don't know if this is because the Documents directory doesn't exist, or because it is empty and empty directories are somehow hidden. Quite a few apps put a "readme" file in their iCloud Documents directory to make it appear. That is what I have done, using an NSFileManager method called something like copyFileFromHere: toThere: creatingIntermediateDirectoriesIfNecessary: true. So that would create Documents/ if necessary.

I don't know what a coordinated write block is

You need to look at the documentation for NSFilePresenter, NSFileCoordinator, and UIDocument. These are required for iCloud files, and also for local files if they are accessible from other apps, including the Files app.

Basically if you're going to read from a file, you need to do a "coordinated read" so that e.g. an instance of your app on another device or the Files app on the same device are told to save the file first. Coordinated writes are the converse. UIDocument simplifies a lot of this. But one important feature of these coordinated things is that they give you a potentially different path to access, in place of the path that you think you want to use. The point is that if there are conflicts between different devices you can have multiple versions of each file.

Having said all that - I don't think it would explain fopen(w) returning "no such file or directory".

It there a particular naming convention for the container name?

I would suggest reverse-dns i.e. com.your-business-name.your-app-name.this-container-name.

I am now able to write my files to iCloud. My big break thru was realizing the a file's URL (returned from the Swift code) needs to be modified (replace, for example "%20" with " ") before using them as a file name in the fopen() call.

Now, I need to figure out why these files are not visible in the Files app. The files are visible on my iPhone via Settings->Accounts->icloud->Manage Storage->. And, I don't yet see them being uploaded to my desktops (either Windows or Mac). Still, pretty good progress for a couple days effort!

My big break thru was realizing the a file's URL (returned from the Swift code) needs to be modified (replace, for example "%20" with " ") before using them as a file name in the fopen() call.

You should be using NSURL.getFileSystemRepresentation to do that.

I need to figure out why these files are not visible in the Files app.   I don't yet see them being uploaded to my desktops

Possibly because of the lack of a coordinated write.

Thanks so much for the help!

"Open file error: No such file or directory" upon opening iCloud file
 
 
Q