FMX.Address Book Sample

From RAD Studio Code Examples
Jump to: navigation, search

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.

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

  1. Navigate to the location given above, and open:
    • Delphi: Contacts_Delphi.dproj
    • C++: Contacts_CPP.cbproj
  2. Select Android or iOS Device as the Target Platform.
  3. Select the device.
  4. 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):

Addr Book.png

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:
    • Select a contact to delete, and then tap Addre Trash.png
  • Refresh the contact list:
    • Tap Addr Refresh.png

The Add Contact Tab

On this tab, you can add a new contact to Address Book.

Add Contact.png

To add a contact

  1. Enter the appropriate information in the Enter First Name, Enter Last Name, or Enter Work Email field.
  2. 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.
  3. Optionally, from the Select Group list, select a group where to add the newly created contact.
  4. 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

  1. In the Enter Group Name text box, enter the new group name.
  2. Tap Addr Add.png

To remove an existing group

  1. From the Select Group list, select a group to remove.
  2. Tap Addre Trash.png.

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:
    • TListView is filled with contacts.
    • TComboBox is filled with groups defined in the Address Book.
  • 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

See Also