Phone Contacts

Before you start:

  • You should have iOS 9.0 and higher.

First of all, you need to make sure that Contacts API is enable inside the plugin editor settings:

Starting from IOS 9 + you need to describe, why you want user to allow an access to the photo library, camera or similar System API like, for example, "Phonebook contacts". The system shows the purpose string while asking the user to allow an access. You can specify the description with the IOS Native editor settings as well:

Implementation

We are providing two ways how you can get user phone contacts.

1) You can get the whole user phone book using the RetrievePhoneContacts methods. See the code snippet below:

SA.IOSNative.Contacts.ContactStore.Instance.RetrievePhoneContacts((result) => {
	if(result.IsSucceeded) {
		Debug.Log("Loaded " + result.Contacts.Count + " contacts");
		IOSMessage.Create("Success", "Loaded " + result.Contacts.Count + " contacts");

		foreach(var contact in result.Contacts) {
			if(contact.PhoneNumbers.Count > 0) {
				Debug.Log(contact.GivenName + " / " + contact.PhoneNumbers[0].Digits);
			}
		}
	} else {
		IOSMessage.Create("Error", result.Error.Code + " / " + result.Error.Message);
	}
});

2) You can show contacts picker UI and let the user select contacts he is willing to share with your App using the ShowContactsPickerUI method. See the code snippet below:

SA.IOSNative.Contacts.ContactStore.Instance.ShowContactsPickerUI ((result) => {
	if(result.IsSucceeded) {
		Debug.Log("Picked " + result.Contacts.Count + " contacts");
		IOSMessage.Create("Success", "Picked " + result.Contacts.Count + " contacts");
		foreach(var contact in result.Contacts) {
			Debug.Log ("contact.GivenName: " + contact.GivenName );
			if(contact.PhoneNumbers.Count > 0) {
				Debug.Log("contact.PhoneNumber: " + contact.PhoneNumbers[0].Digits);
			}
			if(contact.Emails.Count > 0) {
				Debug.Log("contact.Email: " + contact.Emails[0]);
			}
		}
	} else {
		IOSMessage.Create("Error", result.Error.Code + " / " + result.Error.Message);
	}
});

Contact loading result is represented as the ContactsResult object. If contacts have been loaded successfully, you will find Contact objects array inside.

API Reference

ContactStore

namespace SA.IOSNative.Contacts {
	public class ContactStore : SA.Common.Pattern.Singleton<ContactStore> {

		//--------------------------------------
		// Actions
		//--------------------------------------
	
		private event Action<ContactsResult> ContactsLoadResult = delegate {};
		private event Action<ContactsResult> ContactsPickResult = delegate {};


		//--------------------------------------
		// Public Methods
		//--------------------------------------


		public void ShowContactsPickerUI(Action<ContactsResult> callback);
		public void RetrievePhoneContacts(Action<ContactsResult> callback);

	}
}


Contact

namespace SA.IOSNative.Contacts {
	public class Contact  {

		public string GivenName  {get;}
		public string FamilyName {get;}

		public List<string> Emails {get;}
		public List<PhoneNumber> PhoneNumbers {get;}
	}
}

PhoneNumber

namespace SA.IOSNative.Contacts {
	public class PhoneNumber  {
		public string CountryCode  {get;}
		public string Digits {get;}
	}
}

ContactsResult

namespace SA.IOSNative.Contacts {
	public class ContactsResult : SA.Common.Models.Result {

		public List<Contact> Contacts {get;}
	}
}