iOS Tutorial Files and Folders

In iOS each application can only access its own private folders. There is no shared location such as File.DirRootExternal in Android.

iOS file system is case sensitive.

There are several standard folders. Each folder is intended for different usage.

File.DirAssets - This is a read-only folder. The files added to the Files tab in the IDE will be located here.

File.DirTmp
- The location of temporary files. These files will not be backuped by iTunes. The OS can clean this folder from time to time (you are also expected to delete unused files from this folder).

File.DirDocuments
- This folder should be used for user generated documents. It is backed up by iTunes automatically. It is possible to allow the user to access this folder from iTunes as explained below.

File.DirLibrary - This folder should be used for non-user generated persistent files (settings file for example). It is backed up by iTunes automatically.

The user available online storage is limited. If your program creates large persistent files then it might be better to exclude those files from the automatic backup.
This is done by calling File.SetExcludeFromBackup.

The files related methods are similar to B4A methods: http://www.b4x.com/b4i/help/files.html

B4A TextReader and TextWriter types are not available in B4i. If you need to use an encoding other than UTF8 then you should instead use File.ReadString2 or File.WriteString2.

Share your application files with iTunes

As mentioned above File.DirDocuments can be shared through iTunes.
In order to enable this feature you need to add this attribute:
B4X:
#PlistExtra: <key>UIFileSharingEnabled</key><true/>

Now the user will be able to access File.DirDocuments from iTunes:



The project is attached.
 

Attachments

  • iTunesFileSharing.zip
    4.3 KB · Views: 1,066
Last edited:

moster67

Expert
Licensed User
Longtime User
OK thanks.

So if I want to keep a file "private" (not even user should be able to access it), then the best place to save the file (and perhaps to update it later) would be File.DirDocuments as long as UIFileSharingEnabled is set to false, right? I guess that UIFileSharingEnabled will always be be false unless I explicitly set it to true?
 

schimanski

Well-Known Member
Licensed User
Longtime User
I want to make a password-protection for my app and so it is nessessary to have a private file.
I don't use itunes, but I work with copy trans manager. With this tool (Copy Trans apps), i can reach the private files even, if i set
B4X:
#PlistExtra: <key>UIFileSharingEnabled</key><false/>
.

Where can I save a file, which can't reach from the user?
 

schimanski

Well-Known Member
Licensed User
Longtime User
The second screenshot are my files in File.DirDocuments...
 

Attachments

  • Bild1.png
    Bild1.png
    30.6 KB · Views: 911
  • Bild2.png
    Bild2.png
    35.1 KB · Views: 868

Turbo3

Active Member
Licensed User
Longtime User
How would one append to an existing text file?

That is what I used TextWriter for and File.WriteString just overrides the current one with no append option.
==========
Solution
B4X:
Dim msg As String = "Very important file."&CRLF
Dim out As OutputStream = File.OpenOutput(File.DirDocuments, "History.txt", True)
Dim t() As Byte = msg.GetBytes("UTF8")
out.WriteBytes(t, 0, t.Length)
out.WriteBytes(t, 0, t.Length)
out.WriteBytes(t, 0, t.Length)
out.Close
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
I created a folder in File.DirDocuments with

"File.MakeDir(File.DirDocuments, "LOG_FILES")"

and wrote a file to the new folder. I can see the folder in iTunes but I see no way to opened the folder so I can see and copy the new file I created over to the PC. Nothing happens if I try to just copy the folder over to a folder on my PC. It seems iTunes only allows copying over a file but not a folder and has no way to open the folder to see the files inside.

Files that I write to File.DirDocuments (not into a folder) show up and can be copied as shown in the above video but if I create a folder I am not sure how the user can ever get the files written to it.

Is there another way to access the folder without using iTunes? Is there some additional flag I need to set to give iTunes access to this new folder?
 

CryoGenID

Active Member
Licensed User
Longtime User
I have a question regarding DirDocuments:
How do I provide the initial files in there when I publish my app?
In my case I have a SQL file which I need to ship with the app, how do I get B4i to automatically copy the file into that directory:
1) when I am debugging the app
2) when I want to release the app

Thanks a lot for the clarification :)

Best regards,

Christian
 

Turbo3

Active Member
Licensed User
Longtime User
I think the only way is to add files to the app's Files directory and then your app will need to copy it over from DirAssets to DirDocuments.
 

CryoGenID

Active Member
Licensed User
Longtime User
Hello Turbo3,

that was a perfect idea, works like a charm, thank you :)

Best regards,

Christian
 

Turbo3

Active Member
Licensed User
Longtime User
I have the opposite question. How can the user remove files from DirDocuments outside of the app?

With iTunes I only seem to be able to move files in or drag a copy out but not delete a file.
 

CryoGenID

Active Member
Licensed User
Longtime User
Hello Turbo3,

I have used "iBrowse" and could delete files from the directory, I _think_ that I had to close the app though so that the files were not in use...

Best regards,

Christian
 

Turbo3

Active Member
Licensed User
Longtime User
I just realized my mistake. I use a MacBook Pro running BootCamp with Windows 7. So when I pressed the "delete" key it was really the backspace function so was ignored by iTunes.

What I should have pressed and just did was "fn"+"delete" which is the delete function.

Thanks for reporting it could be done. That made me take a second look and remember what pressing the "delete" key really did.
 

Hugh Thomas

Member
Licensed User
Longtime User
In the latest version of my app I set UIFileSharingEnabled to true, and the first time the app runs after installation it puts some small sample files in DirDocuments. In testing the files were visible in iTunes, as were any other files the user saved into DirDocuments using my app.

When I submitted the app for review it was rejected with the following error:

"2.3 Details Your app has the UIFileSharingEnabled key set to true in the info.plist, but the Documents folder includes files and folders".

What am I doing wrong? Aren't you allowed to preload files into DirDocuments?

Hugh
 
Top