Skip to content

Instantly share code, notes, and snippets.

@timefrancesco
Created July 24, 2014 03:16
Show Gist options
  • Select an option

  • Save timefrancesco/829a49476bd10e597049 to your computer and use it in GitHub Desktop.

Select an option

Save timefrancesco/829a49476bd10e597049 to your computer and use it in GitHub Desktop.

Revisions

  1. timefrancesco renamed this gist Jul 24, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. timefrancesco created this gist Jul 24, 2014.
    62 changes: 62 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    /* 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);
    }


    }