Bug 33438 - Problems showing the native IOS person picker and awaiting a selection
Summary: Problems showing the native IOS person picker and awaiting a selection
Status: RESOLVED NORESPONSE
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 1.5.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-08-27 16:23 UTC by Erik Renaud
Modified: 2016-04-13 19:29 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Erik Renaud 2015-08-27 16:23:55 UTC
Imagine a forms app with a button on a form, which through the dependency service calls some code in the native ios code.

I do not know why this code fails
1 - I was able to get the person picker to show up (but now it doesn`t anymore... do not know why)
2 - those times when it did show up, when i selected a person, the method never returned to my method (like if the await task thing was broken).

public async Task<ContactInfo> GetContactThroughPickerAsync()
        {
            var asyncPeoplePicker = new Extensions.AsyncPeoplePicker();
            var person = await asyncPeoplePicker.SelectPersonAsync(GetNativePresentationController());

            if (person == null) return null;
            var result = new ContactInfo()
            {
                FirstName = person.FirstName,
                LastName = person.LastName,
                MobilePhoneNumbers = person.GetPhones().GetValues(),
                Emails = person.GetEmails().GetValues()
            };
            return result;
        }

        private UIViewController GetNativePresentationController()
        {
            UIViewController viewController = null;
            foreach (var vc in UIApplication.SharedApplication.Windows[0].RootViewController.ChildViewControllers)
            {
                if (vc is UIViewController)
                    viewController = (UIViewController)vc;
            }

            return viewController;
        }



Here is the code for the AsyncPeoplePicker class (taken from http://forums.xamarin.com/discussion/24628/abpeoplepickernavigationcontroller-s-selectperson-event)
    public class AsyncPeoplePicker
    {
        public ABPeoplePickerNavigationController PickerController { get; private set; }
        private readonly PickerDelegate pickerDelegate;
        private TaskCompletionSource<ABPerson> tcs;
        private readonly bool isIos8 = UIDevice.CurrentDevice.CheckSystemVersion(8, 0);

        public AsyncPeoplePicker()
        {
            PickerController = new ABPeoplePickerNavigationController();
            pickerDelegate = new PickerDelegate();
            pickerDelegate.SelectPerson += PickerDelegateOnSelectPerson;
            pickerDelegate.Cancel += PickerDelegateOnCancel;

            if (isIos8)
            {
                PickerController.Delegate = pickerDelegate;
            }
            else
            {
                PickerController.SelectPerson2 += (sender, args) =>
                {
                    pickerDelegate.FireSelectPerson(args.Person);
                };

                PickerController.Cancelled += (sender, args) =>
                {
                    pickerDelegate.FireCancelled();
                };
            }
        }

        public Task<ABPerson> SelectPersonAsync(UIViewController presentingController)
        {
            tcs = new TaskCompletionSource<ABPerson>();
            //UIApplication.SharedApplication.InvokeOnMainThread(() =>
            //{
                presentingController.PresentViewController(PickerController, true, null);
            //});
            return tcs.Task;
        }

        private void PickerDelegateOnSelectPerson(object sender, ABPeoplePickerSelectPersonEventArgs args)
        {
            if (tcs != null)
            {
                tcs.TrySetResult(args.Person);
                tcs = null;
            }
        }

        private void PickerDelegateOnCancel(object sender, EventArgs eventArgs)
        {
            if (tcs != null)
            {
                tcs.TrySetResult(null);
                tcs = null;
            }
        }

        /// <summary>
        /// iOS 8: delegate replaced ios7 event
        /// </summary>
        private class PickerDelegate : ABPeoplePickerNavigationControllerDelegate
        {
            public event EventHandler<ABPeoplePickerSelectPersonEventArgs> SelectPerson;
            public event EventHandler<EventArgs> Cancel;

            public void FireSelectPerson(ABPerson person)
            {
                var handler = SelectPerson;
                if (handler != null)
                    handler(this, new ABPeoplePickerSelectPersonEventArgs(person));
            }

            public void FireCancelled()
            {
                var handler = Cancel;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }

            public override void DidSelectPerson(ABPeoplePickerNavigationController peoplePicker, ABPerson selectedPerson)
            {
                FireSelectPerson(selectedPerson);
            }

            public override void Cancelled(ABPeoplePickerNavigationController peoplePicker)
            {
                FireCancelled();
            }

            public override bool ShouldContinue(ABPeoplePickerNavigationController peoplePicker, ABPerson selectedPerson)
            {
                return false;
            }
        }
    }
Comment 1 Rui Marinho 2016-01-27 18:00:33 UTC
Thank you for taking the time to submit the bug. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue we would very much appreciate it.

Warm regards,
Xamarin Forms Team
Comment 2 Paul DiPietro [MSFT] 2016-04-13 19:29:33 UTC
Thank you for your bug report. As we have not received the information requested, we cannot properly triage your bug and are marking it resolved.

Warm regards,
Xamarin Forms Team