0

Is there a way in Monotouch how to get ABPerson object from an IntPtr. I'm using ABPeoplePickerNavigationController and its ABPeoplePickerNavigationControllerDelegate. I have to read some properties of selected person in method ShouldContinue. My code looks like this:

ABPeoplePickerNavigationController nc = new ABPeoplePickerNavigationController();
nc.Delegate = new CustomABPeoplePickerNavigationControllerDelegate();

And my custom delegate looks like this:

public class CustomABPeoplePickerNavigationControllerDelegate : ABPeoplePickerNavigationControllerDelegate
{           
   public override bool ShouldContinue (ABPeoplePickerNavigationController peoplePicker, IntPtr selectedPerson)
   {
       // *** HERE I HAVE TO GET ABPerson FROM IntPtr ***
       peoplePicker.DismissModalViewControllerAnimated(false);
       return true;
   }
}   
poupou
  • 43,413
  • 6
  • 77
  • 174

2 Answers2

1

Sadly the ABPerson constructor that accept an IntPtr is internal in MonoTouch. You can either:

  • use reflection to call the .ctor
  • use another API to retrieve the ABPerson instance

but you cannot use inheritance to solve this since the base (ABRecord) .ctor is also internal.

I'll look why this .ctor is internal (afaik many of such .ctor are public in MonoTouch) and, if possible (i.e. if there's no alternatives), fix this for upcoming releases.

EDIT: further reading suggest that you use the SelectPerson event on ABPeoplePickerNavigationController. This will use an internal delegate that will convert the IntPtr into a ABPerson instance, solving your issue :)

poupou
  • 43,413
  • 6
  • 77
  • 174
  • I was using SelectPerson event before but I had to change it to the ABPeoplePickerNavigationControllerDelegate because of this issue: [link](http://stackoverflow.com/questions/4856728/crash-in-abpeoplepicker-when-called-from-another-modal-viewcontroller-and-both-di). If I use SelectPerson event then my delegate is not used anymore (from some reason). So this is not an option for me unfortunately. Please, could you help me with this reflection way ? Is it even possible to call internal protected contructior using reflection? Thanks in advance – Roman Šimeček Aug 08 '11 at 16:35
  • Please fill a bug report on bugzilla.xamarin.com and we'll try to figure out the test solution(s) to get this working. Short term solution remain using reflection. – poupou Aug 08 '11 at 16:38
  • Using the SelectPerson event on ABPeoplePickerNavigationController seems to crash when picking certain people. In particular this Apple contact, https://dl.dropbox.com/u/11374958/Apple%20Inc..vcf – Brad Moore Mar 13 '13 at 09:23
0

Haven't tried if it works but you could:

ABPerson person = peoplePicker.AddressBook.SingleOrDefault(s => s.Handle == selectedPerson) as ABPerson;
Dimitris Tavlikos
  • 8,170
  • 1
  • 27
  • 31
  • It does not know SingleOrDefault method :( But I was trying to iterate through all people and compare their 'Handle' to my selectPerson IntPtr and they dont match. They are probably different pointers. – Roman Šimeček Aug 08 '11 at 16:41
  • SingleOrDefault is an extension method of the System.Linq namespace. You can add that in your file. I'll check on this a bit more. – Dimitris Tavlikos Aug 08 '11 at 17:53
  • This crashes with no debug info :( Actually, this crashes just like it does when you use `peoplePicker.SelectPerson += delegate(object picker_sender, ABPeoplePickerSelectPersonEventArgs picker_e){ }` – Brad Moore Mar 13 '13 at 08:56