Using TAddressBook to create, access and manage contacts on iOS and Android

Posted by on in Programming

RAD Studio 10.1 Berlin includes TAddressBook, a new FireMonkey component for creating, accessing and managing contacts on iOS and Android. 

We have a great new demo that shows you how to create a new contact, access all on-device contacts and create and manage groups.

 

 

Object Pascal Demo:

https://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RADStudio_Berlin/Object%20Pascal/Multi-Device%20Samples/Device%20Sensors%20and%20Services/Address%20Book/Contacts/ 

 

C++ Demo:

https://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RADStudio_Berlin/CPP/Multi-Device%20Samples/Device%20Sensors%20and%20Services/Address%20Book/Contacts/ 

 

You can access the documentation for this demo by going to: http://docwiki.embarcadero.com/CodeExamples/Berlin/en/FMX.Address_Book_Sample 

 

// Fill the contact list
procedure TForm1.FillContactList(Source: TAddressBookSource);
var
  I: Integer;
  Contacts: TAddressBookContacts;
begin
  Contacts := TAddressBookContacts.Create;
  try
    AddressBook1.AllContacts(Source, Contacts);
    ListViewContacts.BeginUpdate;
    try
      ListViewContacts.Items.Clear;
      for I := 0 to Contacts.Count - 1 do
        AddListViewItem(Contacts.Items[I]);
    finally
      ListViewContacts.EndUpdate;
    end;
  finally
    Contacts.Free;
  end;
end;

 

 

 

// Add a new contact
procedure TForm1.ActionAddContactExecute(Sender: TObject);
var
  Contact: TAddressBookContact;
  eMails: TContactEmails;
begin
  Contact := AddressBook1.CreateContact(FDefaultSource);
  try
    try
      Contact.FirstName := edtFirstName.Text;
      Contact.LastName := edtLastName.Text;
      // Add the work email address
      eMails := TContactEmails.Create;
      try
        eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkMail.Text);
        Contact.eMails := eMails;
      finally
        eMails.Free;
      end;

      AddressBook1.SaveContact(Contact);
    except
      on E: EAddressBookException do
        ShowMessage('Cannot create the contact.' + E.Message);
    end;

    // Add the contact to the selected group, if any
    try
      if InRange(ComboBox1.ItemIndex, 0, FGroups.Count - 1) then
        AddressBook1.AddContactIntoGroup
          (FGroups.Items[ComboBox1.ItemIndex], Contact);
    except
      on E: EAddressBookException do
        ShowMessage('Cannot add the created contact to group.' + E.Message);
    end;

 

 

// Add a new group
procedure TForm1.ActionAddGroupExecute(Sender: TObject);
var
  Group: TAddressBookGroup;
begin
  Group := AddressBook1.CreateGroup(FDefaultSource);
  try
    Group.Name := edtGroupName.Text;
    try
      AddressBook1.SaveGroup(Group);
      edtGroupName.Text := '';
      TabControl1.ActiveTab := TabItemContacts;
    except
      on E: EAddressBookException do
        ShowMessage('Cannot add the group.' + E.Message);
    end;
    FillGroupList(FDefaultSource);
  finally
    Group.Free;
  end;
end;

 

 

 

 

Shown: Demo application running on an iPhone 6s with three of the premium styles that are part of the Premium Style Bonus Pack.

 

Shown: FireUI Designer, showing the Master form with native Android styling and a created Android 5" Phone view with one of the premium Android styles loaded into a TStyleBook component. 

 



About
Gold User, Rank: 5, Points: 558
Senior Product Manager, RAD Studio

Comments

Check out more tips and tricks in this development video: