Forum Xamarin.iOS

Adding a contact: no review form shown

XavierPerseguersXavierPerseguers CHMember ✭✭✭

Hi there, I used to add contacts programmatically using ABPerson but since I'm now only supporting iOS 9 and iOS 10, I want to move to using CNContact. Another reason for switching is that I figured out that suddenly I cannot add contacts anymore using ABPerson.

My application is using Xamarin.Forms and I call this method from my PCL using dependency, just as usual.

My adapted code is as follows (member being a domain object of mine):

var contact = new CNMutableContact ();

// Common properties
contact.GivenName = member.FirstName;
contact.MiddleName = member.MiddleName;
contact.FamilyName = member.LastName;
contact.Birthday = new NSDateComponents () {
    Day = member.Birthday.Day,
    Month = member.Birthday.Month,
    Year = member.Birthday.Year
};
contact.OrganizationName = member.Company;
contact.JobTitle = member.ProfessionTitle;

// TODO: other properties

// Store the contact
var store = new CNContactStore();
var editor = CNContactViewController.FromNewContact (contact);

editor.ContactStore = store;
editor.AllowsActions = true;
editor.AllowsEditing = true;

// Display picker
var navigation = new UINavigationController (editor);
editor.NavigationItem.LeftBarButtonItem = new UIBarButtonItem ("Done",
    UIBarButtonItemStyle.Plain,
    (s, e) => navigation.DismissViewController (true, null));

// Display the view
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (navigation, true, null);

The part preparing a view and showing it with UIApplication.SharedApplication... used to work but now just does nothing!

Could someone please point me out what I'm obviously forgetting to do? Thanks

Best Answer

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭
    Accepted Answer

    OK, will mark this question as answered since showing up the contact form (and just that) now works, thanks to:

    // Store the contact
    var store = new CNContactStore();
    var editor = CNContactViewController.FromNewContact (contact);
    
    editor.ContactStore = store;
    editor.AllowsActions = true;
    editor.AllowsEditing = true;
    
    var navigation = new UINavigationController(editor);
    UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(navigation, true, null);
    

    Problem to dismiss the form will be asked in a separate post: https://forums.xamarin.com/discussion/95934/how-to-dismiss-a-cncontactviewcontroller-modal

Answers

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭

    Nobody ever tried to export a contact to the address book using iOS9/iOS10 framework? really?

  • EazhilEazhil IEUniversity, Developer Group Leader

    I am facing the same problem too, its just does not show up anything.

  • EazhilEazhil IEUniversity, Developer Group Leader

    What worked for me was, instead of presenting the view controller, I used the UINavigationController.PushViewController(contactViewController,animated:true). This got the CNContactViewController to display.

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭

    Good that it works for you, trying to get it working on my side. Inspiring by https://forums.xamarin.com/discussion/28679/how-to-get-uinavigationcontroller-from-xamarin-forms-on-ios I got this:

                // snip
    
                editor.ContactStore = store;
                editor.AllowsActions = true;
                editor.AllowsEditing = true;
    
                var navController = GetUINavigationController(UIApplication.SharedApplication.KeyWindow.RootViewController);
                navController.PushViewController(editor, animated: true);
    
                return true;
            }
    
            private UINavigationController GetUINavigationController(UIViewController controller)
            {
                if (controller != null) {
                    Console.WriteLine("controller is not null");
                    if (controller is UINavigationController) {
                        Console.WriteLine("found UINavigationController");
                        return controller as UINavigationController;
                    }
                    var children = controller.ChildViewControllers.Count();
                    if (children > 0) {
                        for (int c = 0; c < children; c++) {
                            Console.WriteLine("local iteration {0}: current controller has {1} children", c, children);
                            var child = GetUINavigationController(controller.ChildViewControllers[c]);
                            if (child == null) {
                                Console.WriteLine("no children left on current controller, moving back up");
                            }
                            else if (child is UINavigationController) {
                                Console.WriteLine("returning CustomNavigationRenderer");
                                return child as UINavigationController;
                            }
                        }
                    }
                }
    
                return null;
            }
    

    Unfortunately my RootViewController (a UIViewController) does not have any children and its property UINavigationController is null! Thus navController is null and my app now logically crashes with a null pointer exception when invoking PushViewController.

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭

    OK, found something interesting, going back to code I posted first:

    var navigation = new UINavigationController(editor);
    RootViewController.PresentViewController(navigation, true, null);
    

    works but does not let me go back to my application, so the additional code I had is what prevents the view to show up:

    editor.NavigationItem.LeftBarButtonItem = new UIBarButtonItem ("Done",
        UIBarButtonItemStyle.Plain,
        (s, e) => navigation.DismissViewController (true, null));
    
  • XavierPerseguersXavierPerseguers CHMember ✭✭✭
    Accepted Answer

    OK, will mark this question as answered since showing up the contact form (and just that) now works, thanks to:

    // Store the contact
    var store = new CNContactStore();
    var editor = CNContactViewController.FromNewContact (contact);
    
    editor.ContactStore = store;
    editor.AllowsActions = true;
    editor.AllowsEditing = true;
    
    var navigation = new UINavigationController(editor);
    UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(navigation, true, null);
    

    Problem to dismiss the form will be asked in a separate post: https://forums.xamarin.com/discussion/95934/how-to-dismiss-a-cncontactviewcontroller-modal

Sign In or Register to comment.