Superyeti420's Blog


ABAddressBook Helper
November 20, 2010, 7:48 am
Filed under: MonoTouch

Hey everyone, while working on my latest project, i discovered the need to search the address book for a phone number. Through my travels, I DID discover that it seems this functionality isn’t exposed by default with the native iOS libraries, or if it is, its with NSArrays, and NSObjects which can be slower than native MT components. So with a bit of help from Clancy, i came up with this helper class for the MonoTouch ABAddressBook. Enjoy.


public class ContactHelper
{
public ContactHelper ()
{

}

public static ABPerson PickContact(UIViewController view){

ABPeoplePickerNavigationController picker = new ABPeoplePickerNavigationController();

ABPerson ret = null;

picker.SelectPerson += delegate(object sender, ABPeoplePickerSelectPersonEventArgs e) {
picker.DismissModalViewControllerAnimated(true);
ret = e.Person;
};

picker.Cancelled += delegate {
picker.DismissModalViewControllerAnimated(true);
};

view.PresentModalViewController(picker, true);

return ret;

}

public static ABPerson SearchByPhoneNumber(string phoneNumber)
{
List singlePeople = new List();
phoneNumber = Regex.Replace(phoneNumber,"[^0-9]", "");
ABAddressBook ab = new ABAddressBook();

var people = ab.Where(x=> x is ABPerson).Cast().Where(x=> x.GetPhones().Where(p=> Regex.Replace(p.Value,"[^0-9]", "").Contains(phoneNumber) || phoneNumber.Contains(Regex.Replace(p.Value,"[^0-9]", ""))).Count() > 0).ToArray();
foreach(var person in people)
{
if( singlePeople.Intersect(person.GetRelatedNames().Cast()).Count() <= 0)
singlePeople.Add(person);

}
return singlePeople.ToArray().First();
}

}

Advertisement

1 Comment so far
Leave a comment

ABAddressBook Helper « Superyeti420′s Blog…

Thank you for submitting this entry – Trackback from MonoTouch.Info…

Trackback by MonoTouch.Info




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s



Follow

Get every new post delivered to your Inbox.