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

Status
Not open for further replies.
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

SS-2016-02-22_15.24.19.png


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

  • ContactsUtils.zip
    8.2 KB · Views: 96
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

Expert
Licensed User
Is there a chance this library can support getting/setting a contact's ringtone and SMS tone?
 

AlexMaa

Member
Licensed User
iOS 13.3

cutils.SetNote(cu, "Created by B4i")
cutils.GetNote(cu)

Failed to read contact: Error Domain=CNErrorDomain Code=102 "(null)" UserInfo={CNKeyPaths=(
note
), CNInvalidRecords=(
"<CNContactFetchRequest: 0x282d0bc30: predicate=<CNiOSABContactIdentifiersPredicate: 0x283666200: kind=-[CNContact predicateForContactsWithIdentifiers:], identifiers=(\n \"53FB61F4-2612-41CA-B1B9-0F84B7F0961C:ABPerson\"\n)>, keysToFetch=(\n note,\n identifier\n), unifyResults=1, sortOrder=0>"
)}
Stack Trace: (
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1227356
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 135616
B4i Example -[B4IContacts GetContactByIdentifier::] + 200
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
B4i Example +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
B4i Example -[B4IShell runMethod:] + 448
B4i Example -[B4IShell raiseEventImpl:method:args::] + 1648
B4i Example -[B4IShellBI raiseEvent:event:params:] + 1580
B4i Example +[B4IObjectWrapper raiseEvent:::] + 300
B4i Example -[B4ITableViewDelgate tableView:didSelectRowAtIndexPath:] + 368
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 12377292
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 12376040
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 12377832
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 10539304
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 10472568
UIKitCore 27D3041D-A1DE-3C3A-8DC0-994B5982DC49 + 10668744
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 691816
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 671060
CoreFoundation 1B9B1E61-8CB4-3903-9870-402C3DE959BB + 672544
CoreFoundation CFRunLoopRunSpecific + 464
GraphicsServices GSEventRunModal + 104
UIKitCore UIApplicationMain + 1936
B4i Example main + 128
libdyld.dylib 2E3F4750-8B67-398B-8530-8417651B1718 + 4960
)
 
Status
Not open for further replies.
Top