iOS Tutorial Picking external documents with DocumentPickerViewController

SS-2018-11-15_16.16.20.png
]

iUI8 v1.60 adds a new type named DocumentPickerViewController. This picker allows the user to choose an external document. The document will be imported (copied) to your app file system.

Example:
B4X:
Sub Page1_Click
   DocumentPicker.InitializeImport("picker", Array("public.image"))
   DocumentPicker.Show(Page1, Button1)
   Wait For Picker_Complete (Success As Boolean, URLs As List)
   If Success Then
       ImageView1.Bitmap = LoadBitmap(URLs.Get(0), "")
   End If
End Sub

- List of common document types: https://developer.apple.com/library...es/System-DeclaredUniformTypeIdentifiers.html

- In case of file URLs the URL is converted to a file path automatically.
- Note that you should add #MinVersion: 8 when using iUI8 library.
 

asubias

Member
Licensed User
I have the following error in Xcode:
B4X:
Unknown type name 'B4IDocumentPickerViewController'; did you mean 'UIDocumentPickerViewController'?
B4i says that i have installed version 1.61 but it seems there is not the methods in my header file.

EDIT: I got the problem. The last B4iMacBuilder package contains version 1.51 of iUI8. Is out of date. Please, upload the latest version.
 
Last edited:

OlavRossland

Member
Licensed User
Longtime User
SS-2018-11-15_16.16.20.png
]

iUI8 v1.60 adds a new type named DocumentPickerViewController. This picker allows the user to choose an external document. The document will be imported (copied) to your app file system.

Example:
B4X:
Sub Page1_Click
   DocumentPicker.InitializeImport("picker", Array("public.image"))
   DocumentPicker.Show(Page1, Button1)
   Wait For Picker_Complete (Success As Boolean, URLs As List)
   If Success Then
       ImageView1.Bitmap = LoadBitmap(URLs.Get(0), "")
   End If
End Sub

- List of common document types: https://developer.apple.com/library...es/System-DeclaredUniformTypeIdentifiers.html

- In case of file URLs the URL is converted to a file path automatically.
- Note that you should add #MinVersion: 8 when using iUI8 library.

The first example-code is working perfect.
But...

In this code the Array is ("public.image"). I have changed it to ("public.text") to import a text-file (txt) to get importet to a TextView.
But I'm not able to write the right code.
Anyone have a code suggestion for B4I code?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Works fine here:
B4X:
Private Sub Button1_Click
    DocumentPicker.InitializeImport("picker", Array("public.text"))
    DocumentPicker.Show(B4XPages.GetNativeParent(Me), Sender)
    Wait For Picker_Complete (Success As Boolean, URLs As List)
    If Success Then
        Log(File.ReadString(URLs.Get(0), ""))
    End If
End Sub

It lets me select text files and html files.
It is possible that your file isn't treated as text file even if the extension is txt. This is not windows.
 

OlavRossland

Member
Licensed User
Longtime User
Works fine here:
B4X:
Private Sub Button1_Click
    DocumentPicker.InitializeImport("picker", Array("public.text"))
    DocumentPicker.Show(B4XPages.GetNativeParent(Me), Sender)
    Wait For Picker_Complete (Success As Boolean, URLs As List)
    If Success Then
        Log(File.ReadString(URLs.Get(0), ""))
    End If
End Sub

It lets me select text files and html files.
It is possible that your file isn't treated as text file even if the extension is txt. This is not windows.
Thank you Erel :) :)
 

OlavRossland

Member
Licensed User
Longtime User
Works fine here:
B4X:
Private Sub Button1_Click
    DocumentPicker.InitializeImport("picker", Array("public.text"))
    DocumentPicker.Show(B4XPages.GetNativeParent(Me), Sender)
    Wait For Picker_Complete (Success As Boolean, URLs As List)
    If Success Then
        Log(File.ReadString(URLs.Get(0), ""))
    End If
End Sub

It lets me select text files and html files.
It is possible that your file isn't treated as text file even if the extension is txt. This is not windows.

Anyone have a code suggestion for B4I code? (last example...)

This is my code for opening and see and edit the text document in TextViev.Text - and I got this to work:

Sub Button1_Click
DocumentPicker.InitializeImport("picker", Array("public.text"))
DocumentPicker.Show(Page1, Button1)
Wait For Picker_Complete (Success As Boolean, URLs As List)
If Success Then
TextView1.Text=(File.ReadString(URLs.Get(0), ""))
End If
End Sub

I want to save the same file - in the same place, as I opened in the first example - bot this dont work...
Private Sub Button2_Click
File.WriteString("",URLs.Get(0),TextView1.Text)
End Sub
 
Last edited:
Top