Accessing contacts from Delphi at iOS and Android

Recently I was doing an application that needs to access contacts at the iOS and Android.

Contacts

I found those two pieces of code

1) http://www.fmxexpress.com/access-ios-contacts-with-firemonkey-in-delphi-xe5/

2) http://www.fmxexpress.com/access-android-contacts-manager-with-firemonkey-in-delphi-xe5/

that works.

But you need different unit for each platform and the names of fields are also little but different.

So I built from the one library that works the same at Android and iOS.

You can download them here .

Usage:

1) call function createContactsManager from your code. It will return TContactsManager class. At windows nil is returned. Never use TContactsManager.create as it’s an abstract class.

2) From created object call function ExtractContactsList

3) Property ContactsCount will return number of contacts and each contact can be accessed via property Contacts[index].

Really simple.

Android:

For android you need to go to settings of the project and turn on access to the contacts:

contactsPermissions

 

 

iOS

At the iOS it’s even more simple. No need of special permission. The library contains a dialog that will ask user to grant permissions to read the contacts and if the user agree the application can read data.

 

22 comments

  1. Roland says:

    Very cool. Thanks a lot for sharing. I will give it a try.

  2. Sam says:

    Hello thanks for the tutorial.I am developing an app for pbap. But when i am trying to import this library through eclipse its giving message “No project found”. Please guide me where I am doing wrong. Please help.

  3. Kevin says:

    Hi,

    When calling ExtractContactsList I always get an Error: EAbstractError.
    Please Help 🙂

    • pjstrnad says:

      What Delphi you have and at what line you get that error?

      • Kevin says:

        Embarcadero XE6 and I get the error immediately when calling ExtractContactsList. I call it in the Create Constructor of TContactsManager.

        • pjstrnad says:

          I see .. please can you check warnings when you compile. If there is Abstract procedure you get an warning telling you at which class.

        • Steven says:

          Hi,
          I get the same error in XE5

          then Compiler warning tolds me the TContactsManger.ExtractContacsList would be Abstract

          • pjstrnad says:

            Please not use TContactsManager.create – that’s an abstract class.

            Use createContactsManager which is implemented for each of the platform.

            ok?

          • Steven says:

            thanks…
            i changed it and now it works 🙂

  4. Glenn House says:

    Thanks for working this out – it is just what I needed. However, When I try to compile, I get an error that IOSAddressBookWrap.dcu could not be found. This is in the Uses section of ContactsManager.IOS.pas.

    Is this part of TMS Software’s iCL library? I have that library installed. I am using Delphi XE6.

    Thanks

    • pjstrnad says:

      HI,

      I don’t have Delphi XE6, but you can handle it easy.

      1) remove IOSAddressBookWrap from the uses
      2) Compile
      3) You will probably get error that some function can’t be found – look for it at source code of Delphi XE6.
      4) put to uses the unit that you found the function at.
      5) repeat 2 until you get the code working
      6) send me the change so I can make it compatible.

      ok?

      • Glenn House says:

        The iOSAddressBookWrapper is a unit file you used from the http://www.FMXexpress.com/access-ios-contacts-withFireMonkey-in-delphi-XE5/ example. I had to add this file to the project.

        I also needed to go to Tools-> Options->SDK Manager and add the AddressBook Framework for iOS device.

        I am still working on the app and have not accessed the Address Book yet – but it is compiling and running now.

        I still need to test the Android side of it also.

        • pjstrnad says:

          Thanks.

          • Glenn House says:

            I’ve been testing both iOS and Android and it seems to crash when I call fContactsManager.ExtractContactsList. I looked at your ContactsManager.pas file and this is a virtual function. I am not real familiar with virtual functions, but I think it will inherit the base function which is in either the iOS or Android pas file (depending on which is being compiled). But both compiles crash at the same place (ExtractContactsList).

            Any ideas as to what I may be doing wrong?

            Thanks

          • Glenn House says:

            Do I need DPF Delphi Native iOS components installed for this to work?

          • pjstrnad says:

            You don’t need any additional components.

  5. SteveW says:

    Hi,
    new to delphi.

    How do I use the .pas files. I tried adding as a package but it did not appear in the pallet.

    Can you do a simple few lines of code to get the details and show them.

    Cheers

    SteveW

    • pjstrnad says:

      it’s not a component, but just a unit.

      You put it at the uses part of the unit ContactsManager.

      the when you want to use the contacts just use this code:

      var
      contactsManager:TContactsManager;
      ci:integer;
      c:PContactData;
      begin
      contactsManager:=createContactsManager;
      contactsManager.ExtractContactsList;
      for ci:=0 to contactsManager.ContactsCount-1 do
      begin
      c:=contactsManager[ci];

      // do what ever you want with the C
      end;
      end;

  6. SteveW says:

    Hi Thanks for the reply.

    I am in Delphi xe7 ion & Android 4.1.2.
    I have created a simple one form app.
    Added the 3 files to the folder.
    Added ContactsManager to the uses.
    Added your code above to a button

    On compile to android I get:

    [DCC Error] ContactsManager.Android.pas(55): E2003 Undeclared identifier: ‘SharedActivity’
    [DCC Error] ContactsManager.Android.pas(55): E2029 ‘DO’ expected but identifier ‘getContentResolver’ found
    [DCC Error] ContactsManager.Android.pas(58): E2003 Undeclared identifier: ‘StringToJString’
    [DCC Error] ContactsManager.Android.pas(63): E2003 Undeclared identifier: ‘JStringToString’
    [DCC Warning] ContactsManager.Android.pas(66): W1000 Symbol ‘Add’ is deprecated
    [DCC Error] ContactsManager.Android.pas(103): E2003 Undeclared identifier: ‘StringToJString’
    [DCC Error] ContactsManager.Android.pas(107): E2003 Undeclared identifier: ‘SharedActivity’
    [DCC Error] ContactsManager.Android.pas(107): E2029 ‘DO’ expected but identifier ‘getContentResolver’ found
    [DCC Error] ContactsManager.Android.pas(115): E2003 Undeclared identifier: ‘JStringToString’
    [DCC Error] ContactsManager.Android.pas(116): E2010 Incompatible types: ‘string’ and ‘Integer’
    [DCC Error] ContactsManager.Android.pas(134): E2029 ‘DO’ expected but identifier ‘getContentResolver’ found
    [DCC Error] ContactsManager.Android.pas(160): E2029 ‘DO’ expected but identifier ‘getContentResolver’ found
    [DCC Fatal Error] ContactsManager.pas(70): F2063 Could not compile used unit ‘ContactsManager.Android.pas’

    On IOS I get :

    [DCC Warning] ContactsManager.pas(49): W1000 Symbol ‘TList’ is deprecated
    [DCC Warning] ContactsManager.pas(49): W1000 Symbol ‘TList’ is deprecated
    [DCC Fatal Error] ContactsManager.iOS.pas(26): F1026 File not found: ‘C:\Users\stevew\Documents\Embarcadero\Studio\Projects\phoneContacts\AndroidContactsManagerDemo\iOSAddressBookWrap.dcu’

    Cheers

  7. Alex says:

    Thanks man!
    So helpful.
    Btw do you know how to get phone numbers with TAddressBook?
    There is no sample how to do it, even in sample project, so strange.

    Alex.

Leave a Reply

Your email address will not be published. Required fields are marked *