When developing a social mobile application one common feature is to be able to read the phone contacts. That’s a very typical requirement when you have an invite friend’s screen, send an email, etc.
In this article, I will show you how to get the phone contacts and show them on a Xamarin Forms page.
The result will look like this:
Let’s code
1. Create a Contact model
2. Create an IContactsService interface
3. In the iOS project add the implementation for the contact service
4. In the iOS project add the Contacts Usage Description permission to Info.plist
5. In the Android project add the READ_CONTACTS permission to AndroidManifest.xml
<uses-permission android:name=“android.permission.READ_CONTACTS“ />
6. In the Android project add the implementation for the contact service
Make sure to have Plugin.CurrentActivity and Acr.UserDialogs packages installed.
7. In your Android project add the permission handling options
Add this Permission.Util class into your Android project.
Also the ContactsService.OnRequestPermissionsResult in the OnRequestPermissionsResult MainActivity method.
8. Create a ViewModel and use the Contact service
9. Add the page to display the contacts
You can find the full source code here.
References
https://www.oreilly.com/library/view/ios-9-swift/9781491936689/ch04.html
https://www.c-sharpcorner.com/article/how-to-read-contacts-in-xamarin-ios/
https://github.com/enisn/Xamarin.Forms.Contacts
Xamarin.Tip – Read All Contacts in iOS
Happy contacts!
2 Comments
Brilliant article as always.
What about saving contact? There seems to be nothing out there about saving contact.
Just wondering if have saving contact in mind to add
Thanks
Thanks! Will consider saving when updating the sample
Comments are closed.