UIDocumentPickerMBS In App

I am new to IOS but not to XOJO.

I have an app (on MacOS) that I am moving to IOS. The app needs to read and write external data files that are in the “Files” folder in the device. I have added a picker of class UIDocumentPickerMBS but I can not connect the results of the “documentPickerDidPickDocuments” event handler back to the app with the picker.

Does anyone have advice or an example of using UIDocumentPickerMBS to access an external data file from the app. I need to connect the picker event to the app, but I don’t see how to do this. I think this is a structural problem regarding the sandboxing, and I have failed so far.

Thanks for any help or examples (beyond the MBS example “Document Picker iOS” project, which does not show how to access data in the selected file).

Steve Knapp (aka camper@nccn.net)

Well, if you subclass UIDocumentPickerMBS to get the events implemented, you could keep a property with the reference back to the caller there.

Alternatively you can use AddHandler within the screen you are in.

So here this variant:

Sub Pressed() Handles Pressed
  table1.RemoveAllRows
  
  OpenButton.Enabled = False
  ExportButton.Enabled = False
  
  Var types() As String 
  types.append "image/jpeg"
  types.append ".txt"
  types.append "com.adobe.pdf"
  Var asCopy As Boolean = True
  
  picker = New UIDocumentPickerMBS(UIDocumentPicker.ModeOpen, types, asCopy)
  
  AddHandler picker.documentPickerDidPickDocuments, AddressOf documentPickerDidPickDocuments
  AddHandler picker.documentPickerWasCancelled, AddressOf documentPickerWasCancelled
  
  picker.Present
End Sub

and you add two methods to your screen:

Public Sub documentPickerDidPickDocuments(picker as UIDocumentPickerMBS, URLs() as String)
  If picker.documentPickerMode = picker.ModeExportToService Then
    
    MessageBox "Exported."
    
  Else // import/open
    
    For Each url As String In urls
      Var file as new FolderItem(url, FolderItem.PathModes.URL)
      
      Table1.AddRow file.DisplayName
    Next
    
  end if
  
  OpenButton.Enabled = True
  ExportButton.Enabled = True
End Sub

and a method for cancel:

Public Sub documentPickerWasCancelled(picker as UIDocumentPickerMBS)
  OpenButton.Enabled = True
  ExportButton.Enabled = true
End Sub

this way you get the event directly inside the screen where you calk it from. No subclass needed.

Project added:

Document Picker iOS addhandler.zip (7.8 KB)

1 Like

I have finally figured out my problem, which was due mainly to my being new to IOS.

Thanks to those who replied with help, especially Christian and the MBS team, who sent several solutions.

As a newcomer to IOS Xojo programming, I did not understand several terms, and did not understand the central importance of the term “Screen”. The example “Document Picker iOS.xojo_binary_project” did in fact have the answer for me, but it took some time for me to understand. An important lesson learned.

Steve K. (aka camper)

1 Like

Out of curiosity, how did your new understanding of the importance of the term “Screen” resolve your issue?

Arnaud,

When I moved my working app to IOS from MacOS, I had not studied the differences, hoping to be able to find examples to get me going.
It took me a while to understand in the MBS example (“Document Picker iOS”) that the Property ‘screen’ with Type ‘MainScreen’ made screen inherit the class ‘MainScreen’. This was how to use a subclass within the picker that permits accessing properties (variables in my case) of the main app. This is the connection that I was missing between the picker event handler and variables in the main app. The picker allows me to access the external data file and bring the data values into the variables by referencing ‘screen.variablename’. This is all basic stuff, of course, but it took some time, and several failed attempts, before I made the connection with the example.

Now I discover the 'IOS Guide" in xojo.helpdocsonline.com/, a resource that would have helped before I started the migration and which I will consult in the future.

I am an ancient programmer, 82 yo, who started with Fortran around 1964, now in too much of a hurry.

-Steve K.

Thanks for having taken time to explain. It may help users who are new to the iOS part of Xojo (I’m such a user :wink:).