Recently I was doing an application that needs to access contacts at the iOS and Android.
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:
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.
Very cool. Thanks a lot for sharing. I will give it a try.
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.
HI,
I can’t help you with that as I’m not using Eclipse and Delphi together.
Hi,
When calling ExtractContactsList I always get an Error: EAbstractError.
Please Help 🙂
What Delphi you have and at what line you get that error?
Embarcadero XE6 and I get the error immediately when calling ExtractContactsList. I call it in the Create Constructor of TContactsManager.
I see .. please can you check warnings when you compile. If there is Abstract procedure you get an warning telling you at which class.
Hi,
I get the same error in XE5
then Compiler warning tolds me the TContactsManger.ExtractContacsList would be Abstract
Please not use TContactsManager.create – that’s an abstract class.
Use createContactsManager which is implemented for each of the platform.
ok?
thanks…
i changed it and now it works 🙂
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
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?
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.
Thanks.
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
Do I need DPF Delphi Native iOS components installed for this to work?
You don’t need any additional components.
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
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;
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
You can’t add all units. Add just this unit ContactsManager.
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.