Loading file from IOS Documents

I can load files from CopyFiles with

f = SpecialFolder.Resource("Myfile.txt")

But if I try and load a specific file from Documents my app crashes. f exists and has a path

f = SpecialFolder.Documents.child("Myfile.txt")
If f Is Nil Then
  // user cancelled
  Return
End If

Try
  input = TextInputStream.Open(f)

It gives an i/o error 2

Any suggestions?

Device or Simulator?
Do you have Shared File entitlement turned on?
Should If f Is Nil Then actually say If f = Nil Then

I’m using this perfectly well (in the simulator… cant get my app onto a device at all but thats another story)

For example, for files in documents, I am doing this:

/1 Assemble a list of suitable files for a listbox data set

f = SpecialFolder.Documents
For Each f In  SpecialFolder.Documents.Children
  If f.Name.Right(4).Uppercase = ".TXT"   Then
    listoffiles.Append f.name 
  End If
Next
listoffiles.Sort

2/ User selects one for opening, using the list: (Thefilename is a property of the customcell)

f = SpecialFolder.Documents.child(Me.TheFileName )
If f.name.Right(4) = ".TXT" Then
  GlobalMethods.LoadFile(f)
End If

3/ do something with it in my LoadFile, where tempf is the parameter name

var tis As  TextInputStream
var thewholedocument as string
var tis as 
If tempf <> Nil and tempf.exists Then
  Try
    tis = textinputstream.open(tempf)
    tis.Encoding = encodings.utf8
    thewholedocument = tis.ReadAll
    tis.Close
  Catch
  End Try
  end if
1 Like

I bet you’re getting an IOException. Try wrapping that in a try-catch.

Just out of curiosity, how are you getting the file into Documents? And are you sure it exists?

You don’t need this for the Documents folder that’s associated with your app.

True.
But the code shown here has been copied over from a desktop app
(‘user cancelled’ isnt possible on iOS as there is no file selector)

‘The’ documents folder in iOS isnt available to our apps.
Only the shared one is.
So Martin must have copied files into the shared area manually.
Its possible that there is no file in the sandboxed documents folder.
(My own machine has dozens of redundant sandbox environments for simulated apps as I have tried stuff. )

If f has a path and is pointing to the file how can it not load it?
I dragged the file into the simulator manually and on an iPad I airdropped it

Hi Greg,
You are correct the file isn’t there as when I use Jeff’s code to list the files it doesn’t appear. I have dragged the file into documents and I can see it both on the simulator and iPad. Are there multiple Documents folders?

I’m not sure how it helps me but if I drag the file into the App debug folder on the simulator it finds it and my program loads the file.
I’m not sure how a teacher would distribute their custom file so the App could find that, It would be simpler as a desktop App but then it isn’t a student mobile app :frowning:

I’m assuming that when you dragged the file over, that you put it into the iCloud Documents folder. That folder is entirely different than the Documents that is associated with your app.

Thanks, Greg that finds it correctly :beers:

This is my biggest complaint about iOS apps, and I have another post about it.

‘The’ documents folder that people can save things to from (say) Apple apps, is a centralised documents area that is not available to a Xojo built iOS app.
What you CAN access is the sandboxed Documents area that is for your app only.

People can put things into it only if your app has Shared Documents entitlement,

then:
Plug the iPad into a Mac
Open the iPad as if it were a drive
Look at Files.
Find your app
Expand it, and you see the documents area
Drag files into that.

End users absolutely hate doing this, and 50% of them simply cannot achieve it.

1 Like

Thanks Jeff, that must be an absolute pain for school IT admins. I managed a school with 500 Apple desktops for about 15 years but it was a breeze to handle with servers and Remote Desktop. I wonder what they do now with 1000 iPads and all the students on Mobile?

One simpler way to do it is to put your files on a web site, and give them the URL.
You can use a socket to pull text from the URL and save it locally to your app’s document folder.

Other iOS apps can be registered as the ‘owner’ of file types, so that if you get one by email, it can be ‘given’ the file by iOS
Again, that’s not possible using Xojo - it may be possible using declares.
(Probably there is something in iOSKit, but I dread having to insert that into a project - it’s huge , and (to me) impenetrable.)

1 Like

Check out MobileApplication — Xojo documentation
2020r2 added support for your app opening files/URLs

Its not in iOSKit because it wasnt possible with declares, only for Xojo to do it for us

1 Like

Interesting. SO does adding this to an app make iOS aware that the app has a handler for (eg) TXT or PDF or MYFILETYPE ?

I believe that is the case. But I’ll be honest I haven’t used it yet

You’ll still need to add imported file types to your plist file, similarly to how you would do it with macOS.

In fact you could create them in a Mac project, build the project and then copy the parts that you need.

1 Like

Is this in place of having to do a socket call?

MobileApplication.HandleURL (url as String, identifier as String, annotation as String, openInPlace as Boolean)

Ok after a bit more research I’m guessing this just handles urls you click on from the App and you still need a socket to download. Without an example I managed to get pretty tangled up trying to use it. I can just give the user a field to enter a url and that will probably work with some checking what is coming in

Not sure if you solved this I tried to flow below.

When app starts I look for the file in documents if it is not there then I copy a file from the the copyfile location ( know it exist there because it is already created) I believe the resources from then out the file exists in the documents

Although I have been using GetResource which seems to be going away.

1 Like