RootViewController.PresentViewController does nothing (ContactUI)

XavierPerseguersXavierPerseguers CHMember ✭✭✭

Hey there,

This question is related to this one: https://forums.xamarin.com/discussion/94922/adding-a-contact-no-review-form-shown

Problem: when I create a demo XAML project which basically calls this iOS-specific code when clicking on a button:

public void CreateContact()
{
            var tmpContact = new CNMutableContact();
            tmpContact.GivenName = "John";
            tmpContact.FamilyName = "Doo";
            tmpContact.OrganizationName = "Xamarin Test";

            var tmpStore = new CNContactStore();
            var tmpEditor = CNContactViewController.FromNewContact(tmpContact);

            tmpEditor.ContactStore = tmpStore;
            tmpEditor.AllowsActions = true;
            tmpEditor.AllowsEditing = true;
            tmpEditor.Delegate = new ContactDelegate(this);

            var tmpNavigation = new UINavigationController(tmpEditor);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(tmpNavigation, true, null);
}

Delegate:

    class ContactDelegate : CNContactViewControllerDelegate
    {
        private iOSPhoneFeatureService Service;

        public ContactDelegate(iOSPhoneFeatureService service)
        {
            Service = service;
        }

        public override void DidComplete(CNContactViewController viewController, CNContact contact)
        {
            // We do not care if user chose to add the contact or to cancel the operation, we
            // always want to go back to LionsBase mobile!
            viewController.DismissViewController(true, null);

            if (contact != null)
            {
                // Contact has been created
            }
        }
    }

The Add contact form shows up.

However when I do the same in my real project, after searching for a person, showing its detail and clicking on a popup link to add it to the address book, the exact same code does nothing! The ContactUI form is not displayed, and as such one cannot choose to add the contact to the address book.

Any idea what could be the reason? It used to work and suddenly stopped, that's weird.

Best Answer

  • XavierPerseguersXavierPerseguers CH ✭✭✭
    edited August 2017 Accepted Answer

    Found the cause!

    If I add my "create contact" button on the real person detail page, it works, but if I call the action using a toolbar item, it does not!

    public PersonPage()
    {
        InitializeComponent();
        ToolbarItems.Add(new ToolbarItem("", "ToolbarShare", OnShare));
    }
    
    public async void OnShare()
    {
        var addToAddressBook = "Add to address book";
        var action = await DisplayActionSheet(
            null,
            "Cancel",
            null,
            addToAddressBook
        );
        if (action == addToAddressBook) {
            App.PhoneFeatureService.CreateContact();
        }
    }
    

    If I change that to read:

                if (action == addToAddressBook) {
                    Device.BeginInvokeOnMainThread(() => {
                        App.PhoneFeatureService.CreateContact();
                    });
                }
    

    It then works again.

Answers

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭

    Just tried to move my call to CreateContact() (in the real app) to the main form. It then works! So there is really something with the stack of navigation forms loaded which makes the call fail.

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭
    edited August 2017 Accepted Answer

    Found the cause!

    If I add my "create contact" button on the real person detail page, it works, but if I call the action using a toolbar item, it does not!

    public PersonPage()
    {
        InitializeComponent();
        ToolbarItems.Add(new ToolbarItem("", "ToolbarShare", OnShare));
    }
    
    public async void OnShare()
    {
        var addToAddressBook = "Add to address book";
        var action = await DisplayActionSheet(
            null,
            "Cancel",
            null,
            addToAddressBook
        );
        if (action == addToAddressBook) {
            App.PhoneFeatureService.CreateContact();
        }
    }
    

    If I change that to read:

                if (action == addToAddressBook) {
                    Device.BeginInvokeOnMainThread(() => {
                        App.PhoneFeatureService.CreateContact();
                    });
                }
    

    It then works again.

Sign In or Register to comment.