FMX.Address Book Sample
This demo is an FMX application that shows how to use the TAddressBook component to access the Address Book on an Android or iOS device.
Contents
Location
You can find the Address Book sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples, and then navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\Address Book\Contacts
CPP\Multi-Device Samples\Device Sensors and Services\Address Book\Contacts
- Subversion Repository:
- You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
This sample project illustrates how to display and interact with a device Address Book, including the following techniques:
- Requesting a permission to access a mobile device Address Book (Android or iOS).
- Retrieving contacts and groups from the default source in the Address Book.
- Creation/deletion of contacts.
- Creation/deletion of the contact groups.
How to Use the Sample
- Navigate to the location given above, and open:
- Delphi: Contacts_Delphi.dproj
- C++: Contacts_CPP.cbproj
- Select Android or iOS Device as the Target Platform.
- Select the device.
- Press F9 or choose Run > Run.
When you run the sample demo, the application opens the Address Book tab that displays all contacts available in the device Address Book (default source):
This sub-section describes all tabs you can use to manage the device Address Book.
The Address Book Tab
On this tab, you can perform the following operations:
- Delete a currently selected contact:
- Refresh the contact list:
The Add Contact Tab
On this tab, you can add a new contact to Address Book.
To add a contact
- Enter the appropriate information in the Enter First Name, Enter Last Name, or Enter Work Email field.
- Optionally, select the contact person photo from a device photo library or take the photo from a device camera.
To do this, use the icons next to Add Photo. - Optionally, from the Select Group list, select a group where to add the newly created contact.
- Tap the Add button.
The Add/Remove Group Tab
On this tab, you can add or remove the contact groups.
To add a new group
To remove an existing group
Implementation
Requesting a Permission to Access Address Book
The OnShow event triggers when the application form is shown.
OnShow calls RequestPermission.
The RequestPermission method of the TAddressBook requests a permission to read and change the Address Book of the mobile device if the Address Book is currently available on the current platform.
Filling the Contact and Group Lists
The OnPermissionRequest event of the TAddressBook triggers when requesting for a permission to access the mobile device.
- If the permission is granted:
- If permission is not granted, the application displays a message box, which informs that the user is not allowed to access the Address Book.
Tracking Changes in Address Book
The OnExternalChange event of the TAddressBook triggers after detecting a change to the Address Book.
OnExternalChange invokes the RevertCurrentChangesAndUpdate procedure.
If the Address Book is currently available on the current platform, RevertCurrentChangesAndUpdate reverts all current changes and updates the instance of the TAddressBook.
Uses
- TAddressBook
- TListView
- TListBox
- TComboBox
- TActionList
- TTabControl
- TTakePhotoFromLibraryAction
- TTakePhotoFromCameraAction