B4i Library iContacts + ContactsUtils - Read and write to the contacts store

Erel

Administrator
Staff member
Licensed User
ContactsUtils is a class that together with iContacts library, provide read and write access to the device contacts store.

Note that it is based on the new Contacts framework introduced in iOS 9. So your project should include:
B4X:
#MinVersion: 9


The first method that you will usually call is ContactsUtils.GetContacts. It will asynchronously read all contacts and raise the Available event when the data is available.
By default it will only fetch the name related fields. You can pass an array with additional keys that should be fetched for all contacts.
You can later access more fields but it will be slower as it will need to get the fields from the device store.

Example of filling a TableView with the contacts names and first phone number:
B4X:
Private Sub cutils_Available (Success As Boolean, Contacts As List)
   If Success = False Then
     Msgbox("Error getting contacts", "")
     Log(LastException)
     Return
   End If
   TableView1.Clear
   For Each cu As cuContact In Contacts
     Dim phones As List = cutils.GetPhones(cu)
     Dim phone As String
     If phones.Size > 0 Then
       Dim cp As cuPhone = phones.Get(0) 'Each item is a cuPhone type.
       phone = cp.Number
     Else
       phone = "N/A"
     End If
     Dim tc As TableCell = TableView1.AddTwoLines(cu.DisplayName, phone)
     tc.Bitmap = cutils.GetPhotoThumbnail(cu)
     tc.Tag = cu
   Next
   TableView1.ReloadAll
End Sub
cuContact, cuPhone, cuEmail and cuDate are custom types declared in ContactsUtils.

Writing

This code taken from the example project creates a new contact and then updates its fields:
B4X:
Dim cu As cuContact = cutils.InsertContact("AAJohn", "Due")
Dim phone As cuPhone
phone.Number = "12345678"
phone.PhoneType = "Other"
cutils.SetPhones(cu, Array(phone))
cutils.SetNote(cu, "Created by B4i")
cutils.SetPhoto(cu, LoadBitmap(File.DirAssets, "smiley.gif"))
You can also update fields of existing contacts.

Starting from iOS 10 you need to add a usage description such as:
B4X:
#PlistExtra:<key>NSContactsUsageDescription</key><string>Select a contact.</string>
 

Attachments

Last edited:

PalmZac_LYL

Member
Licensed User
Erel,

Great Library ! :)
I want to add/delete contact with new/existing group. How to add/delete Groups in this library ? Would you give me a sample ? Thanks ! ;)
 

JohnC

Well-Known Member
Licensed User
Is there a chance this library can support getting/setting a contact's ringtone and SMS tone?
 
Top