Created
July 24, 2014 03:16
-
-
Save timefrancesco/829a49476bd10e597049 to your computer and use it in GitHub Desktop.
Load the addressbook and select a person's email in Xamarin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* A simple snippet to Open the Address book in iOS and select a person's email address and | |
add it to the textfield (_addressesTextView) | |
PLATFORM: XAMARIN iOS | |
*/ | |
ABPeoplePickerNavigationController _contacts; | |
partial void AddEmail (NSObject sender) | |
{ | |
if (_contacts == null) | |
_contacts = new ABPeoplePickerNavigationController (); | |
_contacts.ModalPresentationStyle = UIModalPresentationStyle.FormSheet; | |
PickerDelegate pickerDeleg = new PickerDelegate(); | |
pickerDeleg.emailSelected += (email) => { | |
if (_addressesTextView.Text.Length > 0) | |
_addressesTextView.Text = _addressesTextView.Text + "," + email; | |
else | |
_addressesTextView.Text = email; | |
}; | |
_contacts.Delegate = pickerDeleg; | |
this.PresentViewController(_contacts,true,null); | |
} | |
//People Picker Delegate | |
public class PickerDelegate:ABPeoplePickerNavigationControllerDelegate | |
{ | |
public delegate void EmailSelectedDelegate(string email); | |
public event EmailSelectedDelegate emailSelected; | |
public override bool ShouldContinue (ABPeoplePickerNavigationController peoplePicker, IntPtr selectedPerson, int propertyId, int identifier) | |
{ | |
if (propertyId == 4) { //kABPersonEmailProperty | |
//this is a bug in XAMARIN, check this: https://bugzilla.xamarin.com/show_bug.cgi?id=16072 | |
ABPerson person = Runtime.GetINativeObject<ABPerson> (selectedPerson,false); | |
var emails = person.GetEmails(); | |
int index = emails.GetIndexForIdentifier(identifier); | |
var values = emails.GetValues(); | |
emailSelected(values[index]); | |
peoplePicker.DismissViewController(true,null); | |
} | |
return false; | |
} | |
public override void Cancelled (ABPeoplePickerNavigationController peoplePicker) | |
{ | |
peoplePicker.DismissViewController(true,null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment