Saving to Documents Folder

Why can’t you see the files you create when you save to the Documents folder? I tried in the Simulator and on my iPad

Var file As FolderItem
file = SpecialFolder.Documents.Child(“MyFile.CWPTEXT”)
Var tout As TextOutputStream = TextOutputStream.Create(file)
tout.Encoding = Encodings.UTF8
tout.Write(STUFF)
tout.Close

Sadly, iOS devices don’t have ‘A’ documents folder.
Every app gets its own, for reasons.

If YOU want to see the files while debugging, pause the app and examine the path of specialfolder.documents in the debugger
Then using Finder, choose the ‘Go’ menu , ‘go to folder’, and paste in the path
You will be horrified to find that OSX has been creating dozens of temporary folders in applicationdata, representing sandboxed apps that you debug.
One of them is a ‘virtual environment’ for your app, and you will find a folder there representing its documents folder.

For your users, if THEY want to see these documents on a device, you must enable the file sharing attribute for your app.
When you do that, the user can interact with that documents folder by:
Plugging the device into a Mac with a cable
The device shows up in Finder looking like an external drive.
They select that, and then in Finder, choose ‘Apps’ on that device
Your app will appear as one that can share files.
Expand that, and you can drag and drop files to and from that area.

IMHO, the process is terrible, and I really wish there was simple access to a user-level documents area instead of an app-level documents folder.
I thought ‘Files’ would meet that need, but (maybe its Xojo) I cannot find a way to use that.

Hi Jeff,
Thanks for the suggestion and thoughts.
Hmmm, so in my situation I have been converting my Crossword program to mobile. Teachers use the creator program to make a custom Xword for students to play on their iPad. I thought the teacher could Airdrop the file to students which launches the Player App. This is working nicely. The last part of the puzzle was if the student doesn’t finish I thought they could save the puzzle and load it later or pick from puzzles that have been airdropped to play.
The other issue I am now finding is every time I airdrop a file to the iPad it always launches the App which I guess is the right behaviour but it never stores the file for later use and a teacher isn’t going to constantly airdrop files to each student.
Maybe they have to get the students to access via iCloud… more investigation needed… doesn’t seem very smooth.
When I was teaching I set my lesson up by placing everything they needed in the Server shared folder, they logged into their desktop the shared volume mounted on everyone’s desktop and access was easy and need one effort and no explanation… progress?

if the student doesn’t finish I thought they could save the puzzle and load it later

easy enough.
Save a file in documents called ‘current’
If it exists when the app opens, open it the saved file and delete it
If the app is deactivated or closed, save progress.

Maybe they have to get the students to access via iCloud

(There are ways in iOSLib apparently, but I have never personally managed to get it working.)

1 Like

FWIW, the files saved to Documents are also available in the Files app when you select “On My Phone”. There’s a folder there for your app which has the docs inside it.

Opting in with the Files App

Before your files can appear in the Files app, you must indicate that your app supports Open in Place and File Sharing Enabled. These options are configured using keys in your Info.plist file. The first key is UIFileSharingEnabled, which enables iTunes sharing of files in your Documents folder. The second key is LSSupportsOpeningDocumentsInPlace, which grants the local file provider access to files in your Documents folder. Add these keys to your Info.plist and set their values to YES.

Source https://bignerdranch.com/blog/working-with-the-files-app-in-ios-11/

Does this mean the app has to do something special, or is it just allowing ‘other apps to see my documents’ on the understanding that they will be allowed to amend them?

I have no idea, never implemented this in one of my apps. I just linked an article that is in my read list. :slightly_smiling_face:

The FileSharing key can be added by setting the File Sharing capability:

image

Do I need these Plist additions as well as Filesharing on in the IDE. It didn’t seem to make a difference??

Since I got the ‘shared documents’ part working on my own apps I checked what settings I have set up.

In Xojo, I ticked the ‘File Sharing’ option in the IDE , using the gear icon I eventually found when I had ‘iOS’ selected. (pretty unintuitive place to find this)
But that was enough to get the app to display documents when plugged into a mac.

My Xojo doesn’t have an entitlement setting for SupportsOpeningDocumentInPlace
Your screenshot doesnt look like my IDE: is it Xojo or am I out of date?

To get other values into the info.plist, I have created a file called info.plist with a few items in it, and dragged that into my project.

I havent tested the ‘inplace’ yet, but I have managed to compile an app that has that set , by using this ‘embedded’ plist:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>UIFileSharingEnabled</key><true/>
<key>LSSupportsOpeningDocumentsInPlace</key><true/>
</dict>
</plist>

When you say ‘it makes no difference’ do you mean file sharing, or the ‘opening in place’ thing that would allow other apps to see your documents?

Screenshot was from Xcode where I was editing the plist.

If I have my App open and airdrop a file to the iPad it recognises the filetype association but doesn’t always open the file. Sometimes it crashes the App. If the App isn’t open it now launches the app and open the file.
I did your save current Crossword suggestion which works well.

I am using your plist now but when I try and access files on the iPad iCloud drive I get an Error accessing File message?

When I drag n drop files from my desktop onto the simulator it works though?

In the IDE do I have to have iCloud entitlements on? It tried this but then the App wouldn’t build.

How do you even do that, error or not?

When I drag n drop files from my desktop onto the simulator it works though?

Yikes. Didnt even know such a thing was possible :slight_smile:
What action would that simulate ‘in real life’?

I thought that might be like Airdropping??

I mounted the drive on my desktop, added some files.
Then I logged into my iCloud account on the iPad and the files show up.
I do a long press on the file and select Share, my app appears as one of the share options and then I get the error. :slight_smile:

Ah.
I confess I have iCloud turned off after it totally wiped my machine of all documents a couple of years ago.
I cannot trust it.
Maybe I’ll try setting it up on a spare machine.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.